Change values in associative array

Let’s say I have an array:


$array = array
(
  'key1' => 'value1',
  'key2' => 'value2',
  'key3' => 'value3',
  'key4' => 'value4',
  'key5' => 'value5',
);

How can I change a specific value in the array? For example, I’d like to change the value for key3:


$array = array
(
  'key1' => 'value1',
  'key2' => 'value2',
  'key3' => 'SOME NEW VALUE',
  'key4' => 'value4',
  'key5' => 'value5',
);


$array['key3'] = 'new value';

I knew about that. But what if you don’t know the key? Can you access the values by numbers, i.e.:


$array[2] = 'new value';

Sorry for the confusion.

What you mean if you don’t know the key of the element of which you want to change the value?? How that can happen? If your array is all associative then you need to use the key itself. You cannot go with numbers.

Search the array for the value then change it.

You may find this to be a better solution:

<?php
$array = array
(
  'key1' => 'value1',
  'key2' => 'value2',
  'key3' => 'value3',
  'key4' => 'value4',
  'key5' => 'value5',
);
$temp = array_keys($array);
echo $array[$temp[2]];

foreach(  $array as $k=&$v )
if( $k == "key3") $v="SOME NEW VAL";

Untested but should work.