array_walk

(PHP 4, PHP 5, PHP 7, PHP 8)

array_walk使用用户自定义函数对数组中的每个元素做回调处理

说明

array_walk ( array &$array , callable $callback , mixed $userdata = null ) : bool

将用户自定义函数 funcname 应用到 array 数组中的每个单元。

array_walk() 不会受到 array 内部数组指针的影响。array_walk() 会遍历整个数组而不管指针的位置。

参数

array

输入的数组。

callback

典型情况下 callback 接受两个参数。array 参数的值作为第一个,键名作为第二个。

Note:

如果 callback 需要直接作用于数组中的值,则给 callback 的第一个参数指定为引用。这样任何对这些单元的改变也将会改变原始数组本身。

Note:

参数数量超过预期,传入内置函数 (例如 strtolower()), 将抛出警告,所以不适合当做 funcname

只有 array 的值才可以被改变,用户不应在回调函数中改变该数组本身的结构。例如增加/删除单元,unset 单元等等。如果 array_walk() 作用的数组改变了,则此函数的的行为未经定义,且不可预期。

userdata

如果提供了可选参数 userdata,将被作为第三个参数传递给 callback funcname

返回值

成功时返回 true, 或者在失败时返回 false

错误/异常

如果 callback 函数需要的参数比给出的多,则每次 array_walk() 调用 callback 时都会产生一个 E_WARNING 级的错误。

范例

Example #1 array_walk() 例子

<?php
$fruits 
= array("d" => "lemon""a" => "orange""b" => "banana""c" => "apple");

function 
test_alter(&$item1$key$prefix)
{
    
$item1 "$prefix$item1";
}

function 
test_print($item2$key)
{
    echo 
"$key$item2<br />\n";
}

echo 
"Before ...:\n";
array_walk($fruits'test_print');

array_walk($fruits'test_alter''fruit');
echo 
"... and after:\n";

array_walk($fruits'test_print');
?>

以上例程会输出:

Before ...:
d. lemon
a. orange
b. banana
c. apple
... and after:
d. fruit: lemon
a. fruit: orange
b. fruit: banana
c. fruit: apple

参见

  • array_walk_recursive() - 对数组中的每个成员递归地应用用户函数
  • iterator_apply() - 为迭代器中每个元素调用一个用户自定义函数
  • list() - 把数组中的值赋给一组变量
  • each() - 返回数组中当前的键/值对并将数组指针向前移动一步
  • call_user_func_array() - 调用回调函数,并把一个数组参数作为回调函数的参数
  • array_map() - 为数组的每个元素应用回调函数
  • foreach

User Contributed Notes

emre 12-Jul-2021 10:07
You can change the key or value with array_walk if you use the temporal returned array in global inside the function. For example:

$array = ['a'=>10, 'b'=>20];
$sequence = array ();

$newArray = array_values(array_walk($array, 'fn'));

function fn(&$val,$key){

global $sequence;

$sequence [] = $val;

}

No need to concern about the place of the internal pointer for the baby array. You have now rewinded, 0 based new array, string key one instead.
mystral77 at gmail dot com 24-Jun-2020 02:54
Hello,

If you want to add values with same key from two arrays :

<?php
function add(&$item,$key,$search) {
   
$item += (is_array($search))?((isset($search[$key]))?$search[$key]:0):0;
}

$a = ["orange" => 2, "banana" => 3, "apple" => 1];
$b = ["orange" => 1, "apple" => 4];

array_walk($c,"add",$b);

echo
"<pre>".print_r($c,true)."</pre>";
?>

This will output:

"orange" => 3,
"banana" => 3,
"apple" => 5
taj at yahoo dot fr 27-Feb-2019 11:42
// 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
)
EllisGL 01-Aug-2018 06:15
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);
?>
01001coder at gmail dot com 05-Dec-2017 10:19
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().
vrrivaro at YESIUSEGMAIL dot gmail dot SO dot com 18-Mar-2017 02:40
The output of the example is only correct if viewed through a web browser. If you pass it through to PHP-CLI, you will get to see the additional HTML line breaks, however.
fantomx1 at gmail dot com 22-Aug-2016 08:08
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;
}
fred 30-Dec-2015 09:01
Correction for the speed test from zlobnygrif.

<?php
// Test results
$array1 = test('array_walk');
$array2 = test('array_walk_list_each');
$array3 = test('array_walk_foreach1');
$array4 = test('array_walk_foreach2');

// Check arrays for equal
var_dump($array1 == $array2, $array1 == $array3, $array1 == $array4);

// Test function 1
function array_walk_list_each(&$array, $function, $userData = null) {
  
/* make sure we walk the array each time */
   
reset($array);
    while ( list(
$key, $value) = each($array) )
       
$function($array[$key], $key, $userData);
}

// Test function 2
function array_walk_foreach1(&$array, $function, $userData = null) {
    foreach (
$array as $key => &$value )
       
$function($value, $key, $userData);
}

// Test function 3
function array_walk_foreach2(&$array, $function, $userData = null) {
    foreach (
$array as $key => $value )
       
$function($array[$key], $key, $userData);
}

function
some_function(&$value, $key, $userData) {
   
$value = "$key => $userData";
}

function
test($function, $count = 10000, $arrayElements = 1000) {
    echo
$function, ' ... ';
   
$array = array_fill(0, $arrayElements, "some text value");
  
   
$timer = microtime(true);
    for(
$i = 0; ++$i < $count; )
       
/* change data for each $i */
       
$function($array, 'some_function', 'some user data ' . $i);
   
printf("%.3f sec\n", microtime(true) - $timer);
  
    return
$array;
}
ezhacher at gmail dot com 27-Aug-2014 12:45
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'));
chaley at brtransport dot com 16-Jun-2014 12:46
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);
zlobnygrif at gmail dot com 17-Jul-2013 05:41
Some speed tests

<?php
// Test results
$array1 = test('array_walk');
$array2 = test('array_walk_list_each');
$array3 = test('array_walk_foreach1');
$array4 = test('array_walk_foreach2');

// Check arrays for equal
var_dump($array1 == $array2, $array1 == $array3, $array1 == $array4);

// Test function 1
function array_walk_list_each(&$array, $function, $userData = null) {
    while ( list(
$key, $value) = each($array) )
       
$function($array[$key], $key, $userData);
}

// Test function 2
function array_walk_foreach1(&$array, $function, $userData = null) {
    foreach (
$array as $key => &$value )
       
$function($value, $key, $userData);
}

// Test function 3
function array_walk_foreach2(&$array, $function, $userData = null) {
    foreach (
$array as $key => $value )
       
$function($array[$key], $key, $userData);
}

function
some_function(&$value, $key, $userData) {
   
$value = "$key => $userData";
}

function
test($function, $count = 10000, $arrayElements = 1000) {
    echo
$function, ' ... ';
   
$array = array_fill(0, $arrayElements, "some text value");
   
   
$timer = microtime(true);
    for(
$i = 0; ++$i < $count; )
       
$function($array, 'some_function', 'some user data');
   
printf("%.3f sec\n", microtime(true) - $timer);
   
    return
$array;
}
?>

Output (PHP 5.4.9-4ubuntu2.2 (cli) (built: Jul 15 2013 18:24:39))
=========================
array_walk ... 13.572 sec
array_walk_list_each ... 0.027 sec
array_walk_foreach1 ... 15.356 sec
array_walk_foreach2 ... 17.416 sec
bool(true)
bool(true)
bool(true)

Output (PHP 5.5.0 (cli) (built: Jul 16 2013 17:59:42) - same server)
=========================
array_walk ... 4.776 sec
array_walk_list_each ... 0.006 sec
array_walk_foreach1 ... 4.482 sec
array_walk_foreach2 ... 5.166 sec
bool(true)
bool(true)
bool(true)

PHP 5.5 array_walk looks pretty good but list each is more and more quickly...
brian at access9 dot net 24-May-2013 10:03
array_walk does not work on SplFixedArray objects:
<?php
$array
= new SplFixedArray(2);
$array[0] = 'test_1';
$array[1] = 'test_2';

array_walk($array, function(&$val){
   
$val .= '__';
    return
$val;
});
foreach (
$array as $a) {
    echo
"$a\n";
}
?>

result is:
test_1
test_2
gold[at]evolved.net.nz 27-Mar-2013 10:08
For all those people trying to shoe-horn trim() into array_walk() and have found all these tricks to work around the issue with array_walk() passing 2 parameters to the callback...

Check out array_map().

http://php.net/array_map

It's all sorts of win.

For the record. I'm one of these people and after 15 years of php development I'm pleased to say that there's still things I'm learning. :)  I just found out about array_map() myself...
espertalhao04 at hotmail dot com 26-Dec-2012 11:35
here is a simple and yet easy to use implementation of this function.
the 'original' function has the problem that you can't unset a value.
with my function, YOU CAN!

<?php
function array_walk_protected(&$a,$s,$p=null)
{
    if(!
function_exists($s)||!is_array($a))
    {
        return
false;
    }
   
    foreach(
$a as $k=>$v)
    {
        if(
call_user_func_array($s,array(&$a[$k],$k,$p))===false)
        {
            unset(
$a[$k]);
        }
    }
}

function
get_name(&$e,$i,$p)
{
    echo
"$i: $e<br>";
    return
false;
}

$m=array('d'=>'33','Y'=>55);

array_walk_protected($m,'get_name');

var_dump($m); //returns array(0) { }
?>

i called it array_walk_protected because it is protected against the unexpected behavior of unsetting the value with the original function.

to delete an element, simply return false!!!
nothing else is needed!
unsetting $e, under your created function, will keep the same array as-is, with no changes!

by the way, the function returns false if $a is not array or $s is not a string!

limitations: it only can run user defined functions.
i hope you like it!
manuscle at gmail dot com 06-Dec-2011 07:16
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);
alex_stanhope at hotmail dot com 26-Oct-2011 01:16
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);
}
?>
tufanbarisyildirim at gmail dot com 05-Oct-2011 06:15
Filter an array by using key.

<?php
    $product_1
= 'test';
   
$product_2 = 'test 2';

    function
array_key_filter($array,$callback = 'trim')
    {  
       
$filtered = array();
       
array_walk($array,function ($degeri,$degisken_adi) use (&$filtered,$callback)
        {  
            if(
$callback($degisken_adi))
            {
               
$filtered[$degisken_adi] =  $degeri;
            }  
        });

        return
$filtered;        
    }

   
#using

   
$degiskenler = array_key_filter(get_defined_vars(),function($key)
    {
        return
strpos($key,'product_') === 0;
    });

   
print_r($degiskenler);
?>
output:

Array
(
    [product_1] => test
    [product_2] => test 2
)
Maxim 11-Jul-2011 12:23
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']);
?>
@jfredys 22-Mar-2011 05:07
I was looking for trimming all the elements in an array, I found this as the simplest solution:

<?php
array_walk
($ids, create_function('&$val', '$val = trim($val);'));
?>
http://alex.moutonking.com/wordpress 15-Feb-2011 10:49
For completeness one has to mention the possibility of using this function with PHP 5.3 closures:

<?php
$names
= array("D\'Artagnan", "Aramis", "Portos");
array_walk($names, function(&$n) {
 
$n = stripslashes($n);
});
?>

The trap with array_walk being it doesn't return the array, instead it's modified by reference.
op_adept at yahoo dot co dot uk 02-Dec-2010 03:20
Prefix array values with keys and retrieve as a glued string, the original array remains unchanged. I used this to create some SQL queries from arrays.

<?php

function array_implode_prefix($outer_glue, $arr, $inner_glue, $prefix=false){
   
array_walk( $arr , "prefix", array($inner_glue, $prefix) );
    return
implode($outer_glue, $arr);
}

function
prefix(&$value, $key, array $additional){
   
$inner_glue = $additional[0];
   
$prefix = isset($additional[1])? $additional[1] : false;
    if(
$prefix === false) $prefix = $key;

   
$value = $prefix.$inner_glue.$value;
}

//Example 1:
$order_by = array("3"=>"ASC", "2"=>"DESC", "7"=>"ASC");
echo
array_implode_prefix(",", $order_by, " ");
//Output: 3 ASC,2 DESC,7 ASC

//Example 2:
$columns = array("product_id", "category_id", "name", "description");
$table = "product";

echo
array_implode_prefix(", ", $columns, ".", $table);
//Output:product.product_id, product.category_id, product.name, product.description

//Example 3 (function prefix) won't really be used on its own
$pre= "vacation";
$value = "lalaland";
prefix($value, $pre, array("."));
echo
$value;

//Output: vacation.lalaland

?>
matthew at codenaked dot org 02-Sep-2010 11:24
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"
*/
?>
arekandrei at yandex dot ru 03-Jan-2010 07:30
You can use lambda function as a second parameter:

<?php
array_walk
($myArray, function(&$value, $key){
   
// if you want to change array values then "&" before the $value is mandatory.
});
?>

Example (multiply positive values by two):

<?php
$myArray
= array(1, 2, 3, 4, 5);

array_walk($myArray, function(&$value, $index){
    if (
$value > 0) $value *= 2;
});
?>
rustamabd at gmail dot com 13-Dec-2009 06:32
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);
?>
erelsgl at gmail dot com 04-Mar-2009 03:40
If you want to unset elements from the callback function, maybe what you really need is array_filter.
jab_creations_-at_-yahoo_-dot-_com 26-Nov-2008 04:08
Unfortunately I spent a lot of time trying to permanently apply the effects of a function to an array using the array_walk function when instead array_map was what I wanted. Here is a very simple though effective example for those who may be getting overly frustrated with this function...

<?php
$fruits
= array("Lemony & Fresh","Orange Twist","Apple Juice");

print_r($fruits);
echo
'<br />';

function
name_base($key)
{
 
$name2 = str_replace(" ", "_", $key);
 
$name3 = str_replace("&", "and", $name2);
 
$name4 = strtolower($name3);
 echo
$name4.'<br />';
 return
$name4;
}
echo
'<br />';

$test = array_map('name_base', $fruits);
$fruits_fixed = $test;
echo
'<br />';
print_r($fruits_fixed);
?>
peterzuzek AT gmail DOT com 16-Nov-2008 09:04
I had some problems using this function - it didn't want to apply PHP-defined functions. So I decided to write my own - here it is. I had to use some generic-programming skills, didn't really checked the speed (I think it could be slow)... I believe it could be much better, but I don't know, how - well, I guess multiple array support and recursion would be nice. So?

Prototype:
bool arrayWalk(array &$arry, callback $callback, mixed $params=false)

<?php

   
function arrayWalk(&$arry, $callback, $params=false) {
       
$P=array(""); // parameters
       
$a=""; // arguement string :)

       
if($params !== false) { // add parameters
           
if(is_array($params)) { // multiple additional parameters
               
foreach($params as $par)
                    {
$P[]=$par; }
            }
            else
// just one additional
               
{ $P[]=$params; }
        }

        for(
// create the arguement string
           
$i=0; isset($P[$i]); ++$i
       
)
            {
$a.='$'.chr($i + 97).', '; } // random argument names

       
$a=substr($a, 0, -2); // to get rid of the last comma and two spaces

       
$func=create_function($a, 'return '.$callback.'('.$a.');'); // the generic function

       
if(is_callable($func)) {
            for(
// cycle through array
               
$i=0; isset($arry[$i]); ++$i
           
) {
               
$P[0]=$arry[$i]; // first element must be the first argument - array value
               
$arry[$i] = call_user_func_array($func, $P); // assign the new value obtained by the generic function
           
}
        }
        else
            { return
false; } // failure - function not callable

       
return true; // success!
   
} // arrayWalk()

?>

One big problem I've noticed so far - for example, if you wanted to use str_replace on the array, you'd fail - simply because of the arguement order of str_replace, where the string modified is the third arguement, not the first as arrayWalk requires.

So, still some work left...
diyism 30-Oct-2008 11:18
When i pass the third parameter by reference in php5.2.5,
happened this: Warning: Call-time pass-by-reference has been deprecated - argument passed by value...

And to set allow_call_time_pass_reference to true in php.ini won't work, according to http://bugs.php.net/bug.php?id=19699   thus to work around:

<?php
array_walk
($arrChnOut, create_function('&$v, $k, $arr_rtn', 'if ($k{0}!="_") {$arr_rtn[0]["_".$v[\'ID\']]=$v; unset($arr_rtn[0][$k]);}'), array(&$arrChnOut));
?>
jerk at yoosic dot de 21-Dec-2006 08:05
if you want to modify every value of an multidimensional array use this function used here:

<?php

$array
= array (1=>1, 2=> 2, 3 => array(1=>11, 2=>12, 3=>13));
$text = "test";

function
modarr(&$array, $text) {
        foreach (
$array as $key => $arr) {
                if(
is_array($arr)) $res[$key] = modarr(&$arr,$text);
               
// modification function here
               
else $res[$key] = $arr.$text;
                }
        return
$res;
}

$erg = modarr($array, $text);

print_r($erg); 

?>

result will be_

<?php
Array ( [1] => 1test [2] => 2test [3] => Array ( [1] => 11test [2] => 12test [3] => 13test ) )
?>
ludvig dot ericson at gmail dot com 21-Nov-2005 06:09
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.
Andrzej Martynowicz at gmail dot com 18-Sep-2005 06:03
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.
el_porno at web dot de 27-May-2005 03:03
You want to get rid of the whitespaces users add in your form fields...?
Simply use...:

class SomeVeryImportantClass
{
...
    public function mungeFormData(&$data)
    {
        array_walk($data, array($this, 'munge'));
    }

    private function munge(&$value, &$key)
    {
        if(is_array($value))
        {
            $this->mungeFormData($value);
        }
        else
        {
            $value = trim($value);
        }
    }
...
}

so...
$obj = new SomeVeryImportantClass;
$obj->mungeFormData($_POST);
___
eNc
Enlightened One 07-Apr-2005 11:17
Beware that "array ($this, method)" construct. If you're wanting to alter members of the "$this" object inside "method" you should construct the callback like this:

  $callback[] = &$this;
  $callback[] = method;
  array_walk ($input, $callback);

Creating your callback using the array() method as suggested by "appletalk" results in a copy of $this being passed to method, not the original object, therefor any changes made to the object by method will be lost when array_walk() returns. While you could construct the callback with "array(&$this, method)", I believe this relies on the deprecated runtime pass-by-reference mechanism which may be removed in future releases of PHP. Better to not create a dependence on that feature now than having to track it down and fix it in the future.
bisqwit at iki dot fi 03-Sep-2004 04:54
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.
PHP8中文手册 站长在线 整理 版权归PHP文档组所有