1

I have an array like:

$example = array("car"=>"red", "banana"=>"yellow", etc...)

Is it possible to alter a value of the array by index instead of key value like

$example[0] = "blue";

So that the array now looks like

$example = array("car"=>"blue", "banana"=>"yellow", etc...)
1
  • Strictly speaking I know you can't, but I didn't know if there was a function that I'm not seeing that would allow it. Commented Aug 18, 2011 at 5:28

1 Answer 1

6

you can do this way:

$e = array("car"=>"red", "banana"=>"yellow");
$keys = array_keys($e);
$e[$keys[0]] = 'blue';

var_dump($e);

the output is

array(2) { ["car"]=> string(4) "blue" ["banana"]=> string(6) "yellow" }

Not the answer you're looking for? Browse other questions tagged or ask your own question.