note 45155 deleted from function.array-walk by aidan
| From: | aidan@php.net | Date: | Sat, 28 Aug 2004 03:52:11 +0000 |
| Subject: | note 45155 deleted from function.array-walk by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75475@lists.php.net to get a copy of this message | ||
Note Submitter: bisqwit at iki dot fi
----
Consider this code.
<?php
function xyz(&$v, &$k)
{
$k = $k.'1';
$v = $v.'2';
}
$array = Array('x' => 'a', 'y' => 'b');
array_walk($array, 'xyz');
print_r($array); ?>
Despite that PHP doesn't complain of this code, the code does not accomplish its goal.
array_walk does not allow the array keys to be modified.
They way around this limitation is to resort to this kind of code:
<?php
$array = Array('x' => 'a', 'y' => 'b');
$tmp = Array();
foreach($array as $k => $v)
{
$new_k = $k.'1';
$new_v = $v.'2';
$tmp[$new_k] = $new_v;
}
$array = $tmp;
print_r($array);