note 45155 added to function.array-walk
| From: | bisqwit at iki dot fi | Date: | Fri, 27 Aug 2004 16:03:28 +0000 |
| Subject: | note 45155 added to function.array-walk | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-75435@lists.php.net to get a copy of this message | ||
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);
----
Manual Page -- http://www.php.net/manual/en/function.array-walk.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45155
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45155&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45155&report=yes
Search -- http://master.php.net/manage/user-notes.php