-1

I have an associative array with strings as keys and I want to list all the keys and corresponding values in the order in which it was input. For example:

$arr=array();
$arr['tree']='leaves';
$arr['fruits']='seed';

and output should be like :

keys : tree, fruits
values : leaves,seed

jacquel
  • 946
  • 1
  • 7
  • 10
  • http://php.net/manual/en/function.array-keys.php or http://php.net/manual/en/function.key.php – Bart Haalstra Jan 26 '15 at 12:14
  • 1
    How do you determine that you want to fetch this specific key? On a whim? Or do you have a specific situation and reason why you need to fetch a specific key? – deceze Jan 26 '15 at 12:16
  • Also: [how to get associative array key from numeric index?](http://stackoverflow.com/a/4095838/476) – deceze Jan 26 '15 at 12:17
  • well i just want to populate the keys in the order in which keys are input. – jacquel Jan 26 '15 at 12:25

2 Answers2

2

If you're running a version of PHP that supports array dereferencing:

$key = array_keys($arr)[1];

else

$keys = array_keys($arr);
$key = $keys[1];
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
1

make it associative array you can get both keys and values

$arr=array('tree'=>'leaves','fruits'=>'seeds');


foreach($arr as $key=>$value)
{
   echo $key."====>".$value;
}
Anil Baweja
  • 150
  • 14