PHP finding the nth element in array -



PHP finding the nth element in array -

php function nth item in array.

here frequently.

$hr = array(' ','mr.','ms.','mrs.','dr.','prof.','rev.','sir.',' ',' '); $na = trim($hr[$rs['tbhr']] .' '. $rs['tbfn'] ." ". $rs['tbln']);

(this illustration code don't take literally) $rs = recordset of info $hr integer representing persons honor designation.

what rather assign array item on fly nth function. however, i'm not finding in php. (i've looked it) maybe there improve way? tips?

here i'd do.

$na = trim(nth($rs['tbhr'], array(' ','mr.','ms.','mrs.','dr.','prof.','rev.','sir.','?','?')) ." ". $rs['tbfn'] .' '. $rs['tbln']);

thus removing need create variable $hr. don't want create function either.

[edit] sorry folks, maybe english language not good.

let have pets array(' ', 'dog', 'cat', 'fish', 'bird', 'snakes'); , sally has cat. sally['pet'] = 'cat';

but don't want store word "cat" in our database. instead, want utilize numbers. sally['pet'] = 2;

in php can:

$sally['pet'] = 2; $pets = array(' ', 'dog', 'cat', 'fish', 'bird', 'snakes'); echo "sally's pet ". $pets[$sally['pet']] .".";

that lot of code convert number string. plus, you've got needless assignment of variable $pets;

is there improve way?

if array has integers keys (which does) doing doing correct, ie:

$hr[$rs['tbhr']];

if array uses strings keys array_slice work:

$salutation = array_slice($hr, $rs['tbhr'], 1);

php arrays function

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -