-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

4

2 Answers 2

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];
Sign up to request clarification or add additional context in comments.

2 Comments

Neat :) Side note: Array dereferencing exists since PHP 5.4
thanks the second code works,direct fetching is showing syntax error
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;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.