Forum Moderators: coopster

Message Too Old, No Replies

Using a value in an array if the key name is unknown

echo $array[Second Key];

         

Jeremy_H

3:29 am on Sep 30, 2006 (gmt 0)

10+ Year Member



Hello,

If I was given the following array:

$array = array(
"alpha" => "apple",
"beta" => "orange",
"gamma" => "grape");

I could print any one of the elements by it's key, for example:

echo $array["beta"];

I can also find how many different keys there are by using count, for example:

count($array);

But say I don't know the name of the keys, but I do know there are three different ones, would it be possible to call one up but it's order? Say the first or second key?

Pseudo Code:

echo $array[Second Key];

Thanks

Jordo needs a drink

6:10 am on Sep 30, 2006 (gmt 0)

10+ Year Member



try $array[x] where x = the number of the element you want.

for example $array[0] = the first element.
$array[2] = the third element.

[edited by: Jordo_needs_a_drink at 6:18 am (utc) on Sep. 30, 2006]

coopster

11:20 am on Oct 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You'll need to convert the array to numerical indexes first though. You can do this with array_values() [php.net].