Re: Echo a value from an arrays position

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 5/4/06, Brad Bonkoski <bbonkoski@xxxxxxxxxxxxxx> 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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux