Re: Echo a value from an arrays position

From: Date: Thu, 04 May 2006 12:51:48 +0000
Subject: Re: Echo a value from an arrays position
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-235498@lists.php.net to get a copy of this message
On 5/4/06, Brad Bonkoski <bbonkoski@mediaguide.com> wrote:
Will this also work with an associative array? If this is what he is talking about, I tried it and it does not work....
I think you're correct. This is because PHP arrays are a "mash-up" (as Jochem put it) of numerical indexes/hashtables/assoc. arrays/etc.... Example: [php] $jonas = array('color' => 'blue', 'number' => 'three'); echo $jonas[1]; // prints nothing $jonas = array('color' => 'blue', 'number' => 'three', 1 => 'readme'); echo $jonas[1]; // prints 'readme', even though it's technically the third element [/php] So, your solution might be to first pull all of the array values out into a new array, like this: [php] $jonas = array('color' => 'blue', 'number' => 'three', 1 => 'readme'); $jonas_new = array_values($jonas); echo $jonas_new[1]; // prints 'three', as you'd hoped [/php] p.s. Bonus: If you wanted to get the keys out, use array_keys() And yes, as Jay said, RTFM. It's very very helpful. HTH, John W

« previous php.general (#235498) next »