|
Posted by Ian Hobson on 10/04/07 22:22
Marc Bauer wrote:
> Hi
>
> i'd like to replace an array key, but i cannot figure out how this could
> work.
>
> As an example this array:
>
> $array1 = array('key1' => 'value1', 'key2' => 'value2', 'key3' =>
> 'value3' );
>
> Now, i'd like to replace the 'key2' => 'value2' with 'key4' => 'value4' and
> the array should look like:
>
> $array1 = array('key1' => 'value1', 'key4' => 'value4', 'key3' =>
> 'value3' );
>
> The order of the keys is important... so i must replace on the same array
> position.
>
> Regards
> Marc
>
>
IIRC the order of the keys is NOT defined, except that integer keys
appear in increasing order. If this is right, your approach relies on
implementation details that may change. With that warning....
$array2 = Array();
foreach ($array1 as key -> value) {
if ($key == 'key2') {
$array2['key4'] = 'value4':
} else {
$array2[key] = $value;
}
}
$array1 = $array2;
Regards
Ian
Navigation:
[Reply to this message]
|