It's worth nothing that array_walk can not be used to change keys in the array.
The function may be defined as (&$value, $key) but not (&$value, &$key).
Even though PHP does not complain/warn, it does not modify the key.(PHP 4, PHP 5, PHP 7, PHP 8)
array_walk — Применяет пользовательскую функцию к каждому элементу массива
Функция применяет пользовательскую callback-функцию к каждому
элементу массива array.
Функция array_walk() не зависит от внутреннего
указателя массива array.
Функция array_walk() обойдёт каждый элемент массива
независимо от позиции указателя.
arrayВходной массив.
callback
При вызове функции только с двумя аргументами в callback-функцию
передаётся два аргумента. Первый — значение элемента массива array,
а второй — ключ или индекс элемента.
Замечание:
Первый параметр
callback-функции определяют как ссылку, чтобыcallback-функция работала не с копией, а с исходным значением массива, тогда изменения элементов внутри функции обратного вызова изменят сам исходный массив.
Замечание:
Многие внутренние функции наподобие strtolower() выбрасывают ошибку, если количество переданных аргументов превышает количество параметров функции, поэтому такие внутренние функции нельзя указывать как значение параметра
callback.
Разрешается изменять только значения массива
array; структуру самого массива изменить
нельзя. Программисту нельзя добавлять, удалять или изменять порядок
элементов. Поведение функции становится неопределённым и непредсказуемым,
если callback-функция не соответствует этому требованию.
arg
Необязательный параметр arg,
передаётся в callback-функцию как третий аргумент,
если указали.
Функция возвращает логическое значение true.
Начиная с PHP 7.1.0 функция выбрасывает исключение ArgumentCountError,
если callback-функция требует больше двух параметров, тогда как передаются
только значение и ключ элемента массива, или больше 3 параметров, если также передаётся
аргумент arg. Раньше при таких условиях каждый раз, когда функция
array_walk() вызывала callback-функцию,
генерировалась ошибка уровня E_WARNING.
| Версия | Описание |
|---|---|
| 8.2.0 |
Значение возврата теперь принадлежит типу true; раньше значение принадлежало типу bool.
|
| 8.0.0 |
Теперь функция выдаёт ошибку уровня E_WARNING,
если параметр callback ожидает,
что значение второго или третьего аргумента передадут по ссылке.
|
Пример #1 Пример обработки значений при обходе массива функцией array_walk()
<?php
$fruits = array("d" => "лимон", "a" => "апельсин", "b" => "банан", "c" => "яблоко");
function test_alter(&$item1, $key, $prefix)
{
$item1 = "$prefix: $item1";
}
function test_print($item2, $key)
{
echo "$key. $item2\n";
}
echo "До:…\n";
array_walk($fruits, 'test_print');
array_walk($fruits, 'test_alter', 'фрукт');
echo "\n…и после:\n";
array_walk($fruits, 'test_print');
?>Результат выполнения приведённого примера:
До:… d. лимон a. апельсин b. банан c. яблоко …и после: d. фрукт: лимон a. фрукт: апельсин b. фрукт: банан c. фрукт: яблоко
Пример #2 Пример использования функции array_walk() с анонимной функцией
<?php
$elements = ['a', 'b', 'c'];
array_walk($elements, function ($value, $key) {
echo "{$key} => {$value}\n";
});
?>Результат выполнения приведённого примера:
0 => a 1 => b 2 => c
foreachIt's worth nothing that array_walk can not be used to change keys in the array.
The function may be defined as (&$value, $key) but not (&$value, &$key).
Even though PHP does not complain/warn, it does not modify the key.Calling an array Walk inside a class
If the class is static:
array_walk($array, array('self', 'walkFunction'));
or
array_walk($array, array('className', 'walkFunction'));
Otherwise:
array_walk($array, array($this, 'walkFunction'));I noticed that :
PHP ignored arguments type when using array_walk() even if there was
declare(strict_types=1) .
See this code as an example ...
<?php
declare(strict_types=1);
$fruits = array("butter" => 5.3, "meat" => 7, "banana" => 3);
function test_print(int $item2, $key) {
echo "$key: $item2<br />\n";
}
array_walk($fruits, 'test_print');
?>
The output is :
butter: 5
meat: 7
banana: 3
whilst the expecting output is :
Fatal error: Uncaught TypeError: Argument 1 passed to test_print() must be of the type integer
because "butter" => 5.3 is float
I asked someone about it and they said "this was caused by the fact that callbacks called from internal code will always use weak type". But I tried to do some tests and this behavior is not an issue when using call_user_func().In response to 'ibolmo', this is an extended version of string_walk, allowing to pass userdata (like array_walk) and to have the function edit the string in the same manner as array_walk allows, note now though that you have to pass a variable, since PHP cannot pass string literals by reference (logically).
<?php
function string_walk(&$string, $funcname, $userdata = null) {
for($i = 0; $i < strlen($string); $i++) {
# NOTE: PHP's dereference sucks, we have to do this.
$hack = $string{$i};
call_user_func($funcname, &$hack, $i, $userdata);
$string{$i} = $hack;
}
}
function yourFunc($value, $position) {
echo $value . ' ';
}
function yourOtherFunc(&$value, $position) {
$value = str_rot13($value);
}
# NOTE: We now need this ugly $x = hack.
string_walk($x = 'interesting', 'yourFunc');
// Ouput: i n t e r e s t i n g
string_walk($x = 'interesting', 'yourOtherFunc');
echo $x;
// Output: vagrerfgvat
?>
Also note that calling str_rot13() directly on $x would be much faster ;-) just a sample.Note that using array_walk with intval is inappropriate.
There are many examples on internet that suggest to use following code to safely escape $_POST arrays of integers:
<?php
array_walk($_POST['something'],'intval'); // does nothing in PHP 5.3.3
?>
It works in _some_ older PHP versions (5.2), but is against specifications. Since intval() does not modify it's arguments, but returns modified result, the code above has no effect on the array and will leave security hole in your website.
You can use following instead:
<?php
$_POST['something'] = array_map(intval,$_POST['something']);
?>There is a note about 3 years ago regarding using this for trimming. array_map() may be cleaner for this. I haven't checked the time/resource impact:
$result = array_map("trim", $array);If you want to unset elements from the callback function, maybe what you really need is array_filter.Don't forget about the array_map() function, it may be easier to use!
Here's how to lower-case all elements in an array:
<?php
$arr = array_map('strtolower', $arr);
?>Since array_walk cannot modify / change / reindex keys as already mentioned, i provide this small wrapping function which accomplishes passing array reference and index using closures , "use" keyword.
function indexArrayByElement($array, $element)
{
$arrayReindexed = [];
array_walk(
$array,
function ($item, $key) use (&$arrayReindexed, $element) {
$arrayReindexed[$item[$element]] = $item;
}
);
return $arrayReindexed;
}// We can make that with this simple FOREACH loop :
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
foreach($fruits as $cls => $vls)
{
$fruits[$cls] = "fruit: ".$vls;
}
Results:
Array
(
[d] => fruit: lemon
[a] => fruit: orange
[b] => fruit: banana
[c] => fruit: apple
)I wanted to walk an array and reverse map it into a second array. I decided to use array_walk because it should be faster than a reset,next loop or foreach(x as &$y) loop.
<?php
$output = array();
array_walk($input, 'gmapmark_reverse', $output);
function gmapmark_reverse(&$item, $index, &$target) {
$target[$item['form_key']] = $index;
}
?>
In my debugger I can see that $target is progressively updated, but when array_walk returns, $output is empty. If however I use a (deprecated) call-by-reference:
<?php
array_walk($input, 'gmapmark_reverse', &$output);
?>
$output is returned correctly. Unfortunately there's not an easy way to suppress the warnings:
<?php
@array_walk($input, 'gmapmark_reverse', &$output);
?>
doesn't silence them. I've designed a workaround using a static array:
<?php
$reverse = array();
array_walk($input, 'gmapmark_reverse');
// call function one last time to get target array out, because parameters don't work
$reverse = gmapmark_reverse($reverse);
function gmapmark_reverse(&$item, $index = 0) {
static $target;
if (!$target) {
$target = array();
}
if (isset($item['form_key'])) {
$target[$item['form_key']] = $index;
}
return($target);
}
?>It can be very useful to pass the third (optional) parameter by reference while modifying it permanently in callback function. This will cause passing modified parameter to next iteration of array_walk(). The exaple below enumerates items in the array:
<?php
function enumerate( &$item1, $key, &$startNum ) {
$item1 = $startNum++ ." $item1";
}
$num = 1;
$fruits = array( "lemon", "orange", "banana", "apple");
array_walk($fruits, 'enumerate', $num );
print_r( $fruits );
echo '$num is: '. $num ."\n";
?>
This outputs:
Array
(
[0] => 1 lemon
[1] => 2 orange
[2] => 3 banana
[3] => 4 apple
)
$num is: 1
Notice at the last line of output that outside of array_walk() the $num parameter has initial value of 1. This is because array_walk() does not take the third parameter by reference.. so what if we pass the reference as the optional parameter..
<?php
$num = 1;
$fruits = array( "lemon", "orange", "banana", "apple");
array_walk($fruits, 'enumerate', &$num ); // reference here
print_r( $fruits );
echo '$num is: '. $num ."\n";
echo "we've got ". ($num - 1) ." fruits in the basket!";
?>
This outputs:
Array
(
[0] => 1 lemon
[1] => 2 orange
[2] => 3 banana
[3] => 4 apple
)
$num is: 5
we've got 4 fruits in the basket!
Now $num has changed so we are able to count the items (without calling count() unnecessarily).
As a conclusion, using references with array_walk() can be powerful toy but this should be done carefully since modifying third parameter outside the array_walk() is not always what we want.For those that think they can't use array_walk to change / replace a key name, here you go:
<?php
function array_explore(array &$array, callable $callback)
{
array_walk($array, function(&$value, $key) use (&$array, $callback)
{
$callback($array, $key, $value);
if(is_array($value))
{
array_explore($value, $callback);
}
});
}
/**
* Stolen from: https://stackoverflow.com/questions/13233405/change-key-in-associative-array-in-php
*/
function renameKey(array &$data, $oldKey, $newKey, $ignoreMissing = false, $replaceExisting = false)
{
if (!empty($data))
{
if (!array_key_exists($oldKey, $data))
{
if ($ignoreMissing)
{
return FALSE;
}
throw new \Exception('Old key does not exist.');
}
else
{
if (array_key_exists($newKey, $data))
{
if ($replaceExisting)
{
unset($data[$newKey]);
}
else
{
throw new \Exception('New key already exist.');
}
}
$keys = array_keys($data);
// Fix from EllisGL: http://php.net/manual/en/function.array-search.php#122377
$keys[array_search($oldKey, array_map('strval', $keys))] = $newKey;
$data = array_combine($keys, $data);
return TRUE;
}
}
return FALSE;
}
$array = [
"_10fish" => 'xyz',
"_11fish" => [
"_22" => "a", "b", "c"
],
"someFish" => [
'xyz',
'@attributes' => ['type' => 'cod']
]
];
array_explore($array, function(&$value, $key)
{
// Replace key '@attrutes' with '_attributes'
if('@attributes' === $key)
{
renameKey($value, $key, '_attributes');
}
});
print_r($array);
?>Using lambdas you can create a handy zip function to zip together the keys and values of an array. I extended it to allow you to pass in the "glue" string as the optional userdata parameter. The following example is used to zip an array of email headers:
<?php
/**
* Zip together the keys and values of an array using the provided glue
*
* The values of the array are replaced with the new computed value
*
* @param array $data
* @param string $glue
*/
function zip(&$data, $glue=': ')
{
if(!is_array($data)) {
throw new InvalidArgumentException('First parameter must be an array');
}
array_walk($data, function(&$value, $key, $joinUsing) {
$value = $key . $joinUsing . $value;
}, $glue);
}
$myName = 'Matthew Purdon';
$myEmail = 'matthew@example.com';
$from = "$myName <$myEmail>";
$headers['From'] = $from;
$headers['Reply-To'] = $from;
$headers['Return-path'] = "<$myEmail>";
$headers['X-Mailer'] = "PHP" . phpversion() . "";
$headers['Content-Type'] = 'text/plain; charset="UTF-8"';
zip($headers);
$headers = implode("\n", $headers);
$headers .= "\n";
echo $headers;
/*
From: Matthew Purdon <matthew@example.com>
Reply-To: Matthew Purdon <matthew@example.com>
Return-path: <matthew@example.com>
X-Mailer: PHP5.3.2
Content-Type: text/plain; charset="UTF-8"
*/
?>example with closures, checking and deleting value in array:
<?php
$array = array('foo' => 'bar', 'baz' => 'bat');
array_walk($array, function($val,$key) use(&$array){
if ($val == 'bar') {
unset($array[$key]);
}
});
var_dump($array);