Re: PHP problem with array keys / pointers

From: Date: Sun, 17 Dec 2006 19:47:08 +0000
Subject: Re: PHP problem with array keys / pointers
References: 1 2 3 4 5 6 7 8 9 10 11  Groups: php.general 
Request: Send a blank email to php-general+get-245928@lists.php.net to get a copy of this message
don't hijack an existing thread. (i.e. don't reply to someone elses' post/reply if your asking a new question. you need to learn to use var_dump(), and if you look at the line of code below you should have everything you need to know about numeric array keys: $a = array(2 => "foo"); $b = array("2" => "foo"); var_dump($a, $b, ($a === $b)); julian haffegee wrote: > Hi all, > > I've a problem thats been bothering me for a week now > > I have an array $animals > > keys and values like this > > 1 => cat > 2 => dog > 3 => mouse > 4 => horse > > I want to be able to access them using either a key or a pointer, > however the key/pointer will be a variable > > without variables, if I want dog, I can either use $animals[1] or > $animals['2'] > > but with a variable , eg. $x = 2 > $animals[$x] will always give me 'mouse' > > what syntax do I need to use for $animals[$x] to give me 'dog' when $x=2 ? > > I've tried all manner of apostrophes and escaped apostrophes, but no > luck. Can anyone help me > Thanks for your time! > jules >

« previous php.general (#245928) next »