2

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

7

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.