array_walk_recursive

(PHP 5, PHP 7, PHP 8)

array_walk_recursive对数组中的每个成员递归地应用用户函数

说明

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

将用户自定义函数 callback 应用到 array 数组中的每个单元。本函数会递归到更深层的数组中去。

参数

array

输入的数组。

callback

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

Note:

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

userdata

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

返回值

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

范例

Example #1 array_walk_recursive() 例子

<?php
$sweet 
= array('a' => 'apple''b' => 'banana');
$fruits = array('sweet' => $sweet'sour' => 'lemon');

function 
test_print($item$key)
{
    echo 
"$key holds $item\n";
}

array_walk_recursive($fruits'test_print');
?>

以上例程会输出:

a holds apple
b holds banana
sour holds lemon

注意上例中的键 'sweet' 并没有显示出来。任何其值为 array 的键都不会被传递到回调函数中去。

参见

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

User Contributed Notes

lincoln dot du dot j at gmail dot com 21-Jul-2020 02:24
Normal function solution

//1,2,2,3,6,7,3,1,4,2
$arr=[
    [1,2],
    [2,3],
    6,7,[3,1,[4,2]]
];

function a($array){
    static $res=[];
    foreach($array as $val){
        if(is_array($val)){
            a($val);
        }else{
            $res[]=$val;
        }
    }
    return $res;
}

print_r(a($arr));
mike at mpsharp dot com 13-Jul-2019 04:32
Here's a more general solution to modifying the array to which the leaf belongs.   You can unset the current key, or add siblings, etc.

<?php
/**
 * Modified version of array_walk_recursive that passes in the array to the callback
 * The callback can modify the array or value by specifying a reference for the parameter.
 *
 * @param array The input array.
 * @param callable $callback($value, $key, $array)
 */
function array_walk_recursive_array(array &$array, callable $callback) {
    foreach (
$array as $k => &$v) {
        if (
is_array($v)) {
           
array_walk_recursive_array($v, $callback);
        } else {
           
$callback($v, $k, $array);
        }
    }
}
?>
Dario 01-Oct-2018 06:42
I was looking for how to change values of array since you can't pass array by reference anymore in new PHP versions, here is simple solution

<?php
array_walk_recursive
(
   
$myArray,
    function (&
$value) {
        if (
/*some condition*/) {
           
$value = 'New value';
        }
    }
);
?>

After that $myArray will be altered with new value.
r 08-Apr-2018 02:54
How to modify external variable from inside recursive function using userdata argument.

<?php
$arr
= [
  
'one' => ['one_one' => 11, 'one_two' => 12],
  
'two' => 2
];

$counter = 0;

//Does not persist
array_walk_recursive( $arr, function($value, $key, $counter) {
  
$counter++;
   echo
"$value : $counter"
},
$counter);
echo
"counter : $counter";

// result
// 11 : 1
// 12 : 1
// 2 : 1
// counter: 0

//Persists only in same array node
array_walk_recursive( $arr, function($value, $key, &$counter) {
  
$counter++;
   echo
"$value : $counter"
},
$counter);

// result
// 11 : 1
// 12 : 2
// 2 : 1
// counter : 0

//Fully persistent. Using 'use' keyword
array_walk_recursive( $arr, function($value, $key) use (&$counter) {
  
$counter++;
   echo
"$value : $counter"
},
$counter);
echo
"counter : $counter";

// result
// 11 : 1
// 12 : 2
// 2 : 3
// counter : 3
seductiveapps.com 14-Mar-2018 05:45
usage :
$nd = $newsApp2->dataSources();
//walkArray ($nd, 'walkArray_printKey', 'walkArray_printValue');
// prints the entire array

$x = chaseToPath ($nd, 'RSS_list/English News',false);
walkArray ($x, 'walkArray_printKey', 'walkArray_printValue');
// prints everything under $nd['RSS_list']['English News']

function &chaseToPath (&$wm, $path, $create=false) {
    //var_dump ($create); die();
    //echo '$wm=<pre>'; var_dump ($wm);echo '</pre>'; //die();
    //$path = str_replace ('/', '/d/', $path);
    //$path .= '/d';
    $nodes = explode ('/', $path);
    $chase = &chase ($wm, $nodes, $create);
   
    //echo '$wm=<pre>'; var_dump ($wm);echo '</pre>'; die();
    /*
    $dbg = array (
        '$path' => $path,
        '$nodes' => $nodes,
        '$wm' => $wm,
        '$chase' => $chase
    );
    echo '$dbg=<pre style="background:red;color:yellow;">'; var_dump ($dbg); echo '</pre>';
    */
    //die();
   
   
    $false = false;
    if (good($chase)) {
        $arr = &result($chase);   
        return $arr;
    } else return $false;
}       

function &chase (&$arr, $indexes, $create=false) {
        if (false) {
        echo 'sitewide/functions.php --- $arr=<pre>'; var_dump ($arr); echo '</pre>';
        echo 'sitewide/functions.php --- $indexes=<pre>'; var_dump ($indexes); echo '</pre>';
        echo 'sitewide/functions.php --- $create=<pre>'; var_dump ($create); echo '</pre>';
        }
    $r = &$arr;
    foreach ($indexes as $idx) {
            //echo 'sitewide/functions.php --- $idx=<pre>'; var_dump ($idx); var_dump (array_key_exists($idx,$r)); var_dump ($r); echo '</pre>';
            if (
                    is_array($r)
                    && (
                            $create===true
                            || array_key_exists($idx,$r)
                    )
            ) {
                    if ($create===true && !array_key_exists($idx,$r)) $r[$idx]=array();
                    //echo 'sitewide/functions.php --- $idx=<pre>'; var_dump ($idx); echo '</pre>';
                    $r = &$r[$idx];
            } else {
                    $err = array(
                    'msg' => 'Could not walk the full tree',
                    'vars' => array(
                            '$idx--error'=>$idx,
                            '$indexes'=>$indexes,
                            '$arr'=>$arr
                            )
                    );
                    badResult (E_USER_NOTICE, $err);
                    $ret = false; // BUG #2 squashed
                    return $ret;
            }
    }
   
        //echo 'sitewide/functions.php --- $r=<pre>'; var_dump ($r); echo '</pre>';
    return goodResult($r);
}
seductiveapps.com 14-Mar-2018 05:10
function walkArray (&$a, $keyCallback, $valueCallback, $k='', $level=0, $path='') {
// usage : walkArray ($someRecursiveArray, 'walkArray_printKey', 'walkArray_printValue');
// can handle recursive arrays. a nested array is a recursive array.
// is faster, especially on large arrays, than RecuriveArrayIterator, see speed testing comment at http://php.net/manual/en/class.recursiveiteratoriterator.php
// provides detailed information to callbacks on where in the data we are, something that array_walk_recursive just doesnt do.
// passes data around as pointers, not copies of data.
    if (
        !function_exists($keyCallback)
        && !function_exists($valueCallback)
    ) {
        return badResult (E_USER_ERROR, array (
            'msg' => 'walkArray() was called without $keyCallback or $valueCallback as existing function names. see also : call_user_func() in PHP8中文参考手册.'
        );
    }

    if (!is_array($a)) {
        return badResult (E_USER_ERROR, array (
            'msg' => 'walkArray() was called but $a parameter passed is not an array.'
        ));
    } else {
        foreach ($a as $k=>&$v) {
            $cd = array ( // callback data
                'type' => 'key',
                'path' => $path,
                'level' => $level,
                'k' => &$k,
                'v' => &$v
            );               
            if (!is_null($keyCallback)) call_user_func ($keyCallback, $cd);
            if (is_array ($v)) {
                walkArray ($a[$k], $keyCallback, $valueCallback, $k, $level+1, $path.'/'.$k);
            } else {
                $cd['type'] = 'value';
                if (!is_null($valueCallback)) call_user_func ($valueCallback, $cd);
            }
        }
    }
    return true;
}

function walkArray_printKey ($cd) {
    echo '<div style="background:blue;color:yellow;border-radius:5px;padding:2px;margin-top:5px;">'.PHP_EOL;
    $indent = 20 * $cd['level'];
    echo '<div style="padding-left:'.$indent.'px">'.PHP_EOL;
    echo 'key : '.$cd['k'].'<br/>'.PHP_EOL;
    echo 'path : '.$cd['path'].'<br/>'.PHP_EOL;
    echo '</div>'.PHP_EOL;
    echo '</div>'.PHP_EOL;
}

function walkArray_printValue ($cd) {
    echo '<pre style="background:green;color:white;border-radius:5px;padding:2px;margin-top:2px;">'.PHP_EOL;
    $indent = 20 * $cd['level'];
    echo '<div style="padding-left:'.$indent.'px">'.PHP_EOL;
    echo 'key : '.$cd['k'].'<br/>'.PHP_EOL;
    echo 'path : '.$cd['path'].'<br/>'.PHP_EOL;
    echo 'value : '.$cd['v'].'<br/>'.PHP_EOL;
    echo '</div>'.PHP_EOL;
    echo '</pre>'.PHP_EOL;
}

// the only thing you have to change in this code is the badResult() function, but you could raise an exception or return false
CommentUser 07-Jul-2017 01:55
The following code, which returns back a flattened array of sorts into the $results array, in newer versions of PHP raises the error "PHP Fatal error:  Call-time pass-by-reference has been removed":

<?php

$results
= array();

function
example_function ($item, $key, &$arr_values)
{
   
$arr_values[$key] = $item;
}

array_walk_recursive($data, 'example_function', &$results);

print_r($results);

?>

This can be fixed using an anonymous function:

<?php

$results
= array();

array_walk_recursive($data, function ($item, $key) use (&$results){$results[$key] = $item;});

print_r($results);

?>
lincoln dot du dot j at gmail dot com 03-Jul-2017 06:40
multidimensional array to single array

$array=[1=>[2,5=>[4,2],[7,8=>[3,6]],5],4];
$arr=[];
array_walk_recursive($array, function($k){global $arr; $arr[]=$k;});
print_r($arr);

Output

Array ( [0] => 2 [1] => 4 [2] => 2 [3] => 7 [4] => 3 [5] => 6 [6] => 5 [7] => 4 )
Anonymous 03-Jul-2017 06:39
multidimensional array to single array

$array=[1=>[2,5=>[4,2],[7,8=>[3,6]],5],4];
$arr=[];
array_walk_recursive($array, function($k){global $arr; $arr[]=$k;});
print_r($arr);

Output

Array ( [0] => 2 [1] => 4 [2] => 2 [3] => 7 [4] => 3 [5] => 6 [6] => 5 [7] => 4 )
webdiecinove at gmail dot com 04-Jan-2017 04:34
If you want print a multidimensional array, you could try this (the array I use is just an example).
Then, I use array_walk_recursive with a function that checks the type of items:

$arraytest=array();
$arraytest["firstlev1"] = "First level";
$arraytest["firstlev2"] = array(
                0 => array(
                    "secondlev11"=>"Title1",
                    "secondlev12"=>"Subtitle1",
                    "secondlev13" => array(
                        0 => array("othertitle"=>"other title", "othersubtitle"=>"other sub title"),
                        1 => array("info"=>"Informations", "otherinfo"=>"Other info")
                    )
                ),
                1 => array(
                        "secondlev21"=>"Title2",
                        "secondlev22"=>"Subtitle2"
                )
);

function testitems($theitem,$thekey){
       
    if(is_array($theitem)){
        foreach($theitem as $thekey=> $theitem2){
            echo "<br>the key: $thekey..........the item: $theitem2";
        }
    }
    else{
            echo "<br>the value:  $theitem";
           
    }
       
}

array_walk_recursive($arraytest, 'testitems');
KH 01-Nov-2016 05:43
Example for the use of $userdata:

$userdata is the third parameter of the callback function.
The first parameter is $item, the second is $key, and the third one is $userdata.

<?php
$sweet
= array('a' => 'Apfel', 'b' => 'Banane');
$fruits = array('sü?' => $sweet, 'sauer' => 'Zitrone');

function
test_print($item, $key, $info)
{
    echo
"$key beinhaltet $item $info\n";
}

array_walk_recursive($fruits, 'test_print', '  ... Bio');
?>

generates

a beinhaltet Apfel ... Bio
b beinhaltet Banane ... Bio
sauer beinhaltet Zitrone ... Bio
php at genjo dot fr 13-Mar-2015 05:33
I use RecursiveIteratorIterator with parameter CATCH_GET_CHILD to iterate on leafs AND nodes instead of array_walk_recursive function :

<?php
// Iteration on leafs AND nodes
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($candidate), RecursiveIteratorIterator::CATCH_GET_CHILD) as $key => $value) {
    echo
'My node ' . $key . ' with value ' . $value . PHP_EOL;
}
?>
robin leffmann 28-Jan-2015 03:48
A simple solution for walking a nested array to obtain the last set value of a specified key:

<?php

$key
= 'blah';
$val = null;
array_walk_recursive( $your_array,
                      function(
$v, $k, $u) { if($k === $u[0]) $u[1] = $v; },
                      [
$key ,&$val] );

echo
"$key = $val";

?>
gk at anuary dot com 07-Mar-2014 09:29
array_walk_recursive itself cannot unset values. Even though you can pass array by reference, unsetting the value in the callback will only unset the variable in that scope.

<?php
/**
 * http://uk1.php.net/array_walk_recursive implementation that is used to remove nodes from the array.
 *
 * @param array The input array.
 * @param callable $callback Function must return boolean value indicating whether to remove the node.
 * @return array
 */
function walk_recursive_remove (array $array, callable $callback) {
    foreach (
$array as $k => $v) {
        if (
is_array($v)) {
           
$array[$k] = walk_recursive_remove($v, $callback);
        } else {
            if (
$callback($v, $k)) {
                unset(
$array[$k]);
            }
        }
    }

    return
$array;
}
?>

An up to date implementation of the above function can be looked up from https://github.com/gajus/marray/blob/master/src/marray.php#L116.
Anonymous 21-Nov-2013 10:42
since PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.
Rodrigo Guariento 23-Aug-2013 01:27
Simple array_walk_recursive:

// var example
$myArray = Array(
  Array('keyA1' => '    textA1 ', 'keyA2' => '  textA2     '),
  Array('keyB1' => '    textB1 ', 'sub' =>
        Array('keyB1_sub1' => '      textB1_sub1   '),
          Array('keyB1_sub2' => '      textB1_sub2   ')
      ),
  Array('keyC1' => '    textC1 ', 'keyC2' => '  textC2     '),
  Array('keyD1' => '    textD1 ', 'keyD2' => '  textD2     '),
  Array('keyE1' => '    textE1 ', 'keyE2' => '  textE2     ')
);

// function for "trim" (or your function, use same structure)
function trimming($data) {
  if (gettype($data) == 'array')
    return array_map("trimming", $data);
  else
    return trim($data);
}

// get array
$myArray = array_map("trimming", $myArray);

// show array trimmed
var_dump($myArray);

/*
RESULT

array (size=5)
  0 =>
    array (size=2)
      'keyA1' => string 'textA1' (length=6)
      'keyA2' => string 'textA2' (length=6)
  1 =>
    array (size=3)
      'keyB1' => string 'textB1' (length=6)
      'sub' =>
        array (size=1)
          'keyB1_sub1' => string 'textB1_sub1' (length=11)
      0 =>
        array (size=1)
          'keyB1_sub2' => string 'textB1_sub2' (length=11)
  2 =>
    array (size=2)
      'keyC1' => string 'textC1' (length=6)
      'keyC2' => string 'textC2' (length=6)
  3 =>
    array (size=2)
      'keyD1' => string 'textD1' (length=6)
      'keyD2' => string 'textD2' (length=6)
  4 =>
    array (size=2)
      'keyE1' => string 'textE1' (length=6)
      'keyE2' => string 'textE2' (length=6)

*/
none at of dot your dot biz 03-Jun-2013 10:50
Since this is only mentioned in the footnote of the output of one of the examples, I feel it should be spelled out:

* THIS FUNCTION ONLY VISITS LEAF NODES *

That is to say that if you have a tree of arrays with subarrays of subarrays, only the plain values at the leaves of the tree will be visited by the callback function.  The callback function isn't ever called for a nodes in the tree that subnodes (i.e., a subarray).  This has the effect as to make this function unusable for most practical situations.
chris at willowsconsulting dot ie 23-Oct-2012 10:11
To convert all values of an array in UTF8, do this:

<?php

function convert_before_json(&$item, &$key)
{
  
$item=utf8_encode($item);
}

array_walk_recursive($your_array,"convert_before_json");

?>
cyranix at cyranix dot com 28-Oct-2011 07:45
I needed to add or modify values in an array with unknown structure. I was hoping to use array_walk_recursive for the task, but because I was also adding new nodes I came up with an alternate solution.

<?php

   
/**
     * Sets key/value pairs at any depth on an array.
     * @param $data an array of key/value pairs to be added/modified
     * @param $array the array to operate on
     */
   
function setNodes($data, &$array)
    {
       
$separator = '.'; // set this to any string that won't occur in your keys
       
foreach ($data as $name => $value) {
            if (
strpos($name, $separator) === false) {
               
// If the array doesn't contain a special separator character, just set the key/value pair.
                // If $value is an array, you will of course set nested key/value pairs just fine.
               
$array[$name] = $value;
            } else {
               
// In this case we're trying to target a specific nested node without overwriting any other siblings/ancestors.
                // The node or its ancestors may not exist yet.
               
$keys = explode($separator, $name);
               
// Set the root of the tree.
               
$opt_tree =& $array;
               
// Start traversing the tree using the specified keys.
               
while ($key = array_shift($keys)) {
                   
// If there are more keys after the current one...
                   
if ($keys) {
                        if (!isset(
$opt_tree[$key]) || !is_array($opt_tree[$key])) {
                           
// Create this node if it doesn't already exist.
                           
$opt_tree[$key] = array();
                        }
                       
// Redefine the "root" of the tree to this node (assign by reference) then process the next key.
                       
$opt_tree =& $opt_tree[$key];
                    } else {
                       
// This is the last key to check, so assign the value.
                       
$opt_tree[$key] = $value;
                    }
                }
            }
        }
    }

?>

Sample usage:

<?php

$x
= array();
setNodes(array('foo' => 'bar', 'baz' => array('quux' => 42, 'hup' => 101)), $x);
print_r($x); // $x has the same structure as the first argument
setNodes(array('jif.snee' => 'hello world', 'baz.quux.wek' => 5), $x);
print_r($x); // added $x['jif']['snee'] and modified $x['baz']['quux'] to be array('wek' => 5)

?>
ghoffman at salientdigital dot com 13-Oct-2011 10:29
If you are wanting to change the values of an existing multi-dimensional array, as it says above in the note, you need to specify the first argument as a reference. All that means is, be sure to precede the $item variable with an ampersand (&) as in the good_example below.

Unfortunately the PHP example given doesn't do this. It actually took me a while to figure out why my function wasn't changing the original array, even though I was passing by reference.

Here's the tip: Don't return any value from the function! Just change the value of $item that you passed in by reference. This is rather counter-intuitive since the vast majority of functions return a value.

<?php
// array_walk_recursive fails to change your array unless you pass by reference.
// Don't return values from your filter function, even though it's quite logical at a glance!
function bad_example($item,$key){
   if(
$key=='test'){
       return
'PHP Rocks'// Don't do it
  
}else{
      return
$item// Don't do this either
  
}
}

// array_walk_recursive pass-by-reference example
function good_example(&$item,$key){
   if(
$key=='test'){
       
$item='PHP Rocks'; // Do This!
  
}
}

$arr = array('a'=>'1','b'=>'2','test'=>'Replace This');

array_walk_recursive($arr,'bad_example');
var_dump($arr);
/**
 * no errors, but prints...
 * array('a'=>'1','b'=>'2','test'=>'Replace This');
 */

array_walk_recursive($arr,'good_example');
var_dump($arr);
/**
 * prints...
 * array('a'=>'1','b'=>'2','test'=>'PHP Rocks');
 */

?>

Returning a value from your function does work if you pass by reference and modify $item before you return, but you will eat up memory very, very fast if you try it, even on an example as small as the one here.

One other silly thing you might try first is something like this:

<?php
// Resist the urge to do this, it doesn't work.
$filtered = array_walk_recursive($unfiltered,'filter_function');
?>

Of course, $filtered is just TRUE afterwards, not the filtered results you were wanting. Oh, it ran your function recursively alright, but changed all the values in the local function scope only and returns a boolean as the documentation states.
rob at yurkowski dot net 26-Oct-2010 11:16
If you don't really particularly care about the keys of an array, you can capture all values quite simply:

<?php

$sample
= array('dog' => 'woof', 'cat' => array('angry' => 'hiss', 'happy' => 'purr'), 'aardvark' => 'kssksskss');
$output = array();

// Push all $val onto $output.
array_walk_recursive($sample, create_function('$val, $key, $obj', 'array_push($obj, $val);'), &output);

// Printing
echo nl2br(print_r($output, true));

/*
* Array
* (
*  [0] => woof
*  [1] => hiss
*  [2] => purr
*  [3] => kssksskss
* )
*/
?>

[EDIT BY danbrown AT php DOT net: In a note added by 'FaustoFilho' on 17-MAY-2011, the following information was appended to this note.

[If you intend to use this] "function to extract the last key value of an array, don't forget to insert a currency sign ($) signal before '$output' at 7th line.

This value must be a variable, and if you forgot to assign this signal, your code won't work, displaying an error like this:

Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in /path/to/script.php on line 7."]
bradbeattie at gmail dot com 27-Aug-2010 11:18
The description says "If funcname needs to be working with the actual values of the array, specify the first parameter of funcname as a reference." This isn't necessarily helpful as the function you're calling might be built in (e.g. trim or strip_tags). One option would be to create a version of these like so.

<?php
   
function trim_by_reference(&$string) {
       
$string = trim($string);
    }
?>

The downside to this approach is that you need to create a wrapper function for each function you might want to call. Instead, we can use PHP 5.3's inline function syntax to create a new version of array_walk_recursive.

<?php
   
/**
     * This function acts exactly like array_walk_recursive, except that it pretends that the function
     * its calling replaces the value with its result.
     *
     * @param $array The first value of the array will be passed into $function as the primary argument
     * @param $function The function to be called on each element in the array, recursively
     * @param $parameters An optional array of the additional parameters to be appeneded to the function
     *
     * Example usage to alter $array to get the second, third and fourth character from each value
     *     array_walk_recursive_referential($array, "substr", array("1","3"));
     */
   
function array_walk_recursive_referential(&$array, $function, $parameters = array()) {
       
$reference_function = function(&$value, $key, $userdata) {
           
$parameters = array_merge(array($value), $userdata[1]);
           
$value = call_user_func_array($userdata[0], $parameters);
        };
       
array_walk_recursive($array, $reference_function, array($function, $parameters));
    }
?>

The advantage here is that we only explicitly define one wrapper function instead of potentially dozens.
amoffat at amoffat dot com 04-Jun-2008 05:15
<?
function my_array_map() {
    $args = func_get_args();
    $arr = array_shift($args);
   
    foreach ($args as $fn) {
        $nfn = create_function('&$v, $k, $fn', '$v = $fn($v);');
        array_walk_recursive($arr, $nfn, $fn);
    }
    return $arr;
}
?>

takes an array as the first argument, and functions as the other arguments.  it applies those functions recursively to the array
JW 14-Mar-2008 05:59
This function has a serious bug, which is still not fixed as of the PHP 5.2.5 release. After you call it, it can accidentally modify your original array. Save yourself hours of frustration by reading on.

The bug is here: http://bugs.php.net/bug.php?id=42850, and it looks like it will be fixed for 5.3.

If the array that you walk contains other array elements, they will be turned into references. This will happen even if the callback function doesn't take its first argument by reference, and doesn't do anything to the values.

For example, try this:
<?php
$data
= array ('key1' => 'val1', 'key2' => array('key3' => 'val3'));
function
foo($item, $key){}
var_dump($data);
?>

The original array has no references. Now try this:
<?php
array_walk_recursive
($data,'foo');
var_dump($data);
?>

Now key2 is a reference, not just an array. So if you do this:
<?php
function test($item){$item['key2'] = array();}
test($data);
var_dump($data);
?>

you will see that test modifies $data, even though it shouldn't.

One workaround is to immediately make a deep copy of the array after calling array_walk_recursive, like this:
<?php
function array_duplicate($input) {
  if (!
is_array($input)) return $input;
 
$output = array();
  foreach (
$input as $key => $value) {
   
$output[$key] = array_duplicate($value);
  }
  return
$output;
}
array_walk_recursive($data,'foo');
$data = array_duplicate($data);
var_dump($data);
?>

After doing that, the references are gone.
ik at paulkaspers dot nl 15-Oct-2006 01:46
To egingell at sisna dot com:

There is a small bug in your function, the following line should be changed:
From: if ($value != $saved_value || $saved_key != $key) {
Into: if ($value !== $saved_value || $saved_key !== $key) {

It's a nice function, because I was searching for something to change the keys of a multiple dimension array.
gabrielu at hotmail dot com 21-Feb-2006 01:13
I decided to add to the previous PHP 4 compatible version of array_walk_recursive() so that it would work within a class and as a standalone function.  Both instances are handled by the following function which I modified from omega13a at sbcglobal dot net.

The following example is for usage within a class.  To use as a standalone function take it out of the class and rename it.  (Example: array_walk_recursive_2)

<?php
class A_Class {

function
array_walk_recursive(&$input, $funcname, $userdata = '') {
  if(!
function_exists('array_walk_recursive')) {
    if(!
is_callable($funcname))
      return
false;

    if(!
is_array($input))
      return
false;

    foreach(
$input as $key=>$value) {
      if(
is_array($input[$key])) {
        if(isset(
$this)) {
          eval(
'$this->' . __FUNCTION__ . '($input[$key], $funcname, $userdata);');
        } else {
          if(@
get_class($this))
            eval(
get_class() . '::' . __FUNCTION__ . '($input[$key], $funcname, $userdata);');
          else
            eval(
__FUNCTION__ . '($input[$key], $funcname, $userdata);');
        }
      } else {
       
$saved_value = $value;

        if(
is_array($funcname)) {
         
$f = '';
          for(
$a=0; $a<count($funcname); $a++)
            if(
is_object($funcname[$a])) {
             
$f .= get_class($funcname[$a]);
            } else {
              if(
$a > 0)
               
$f .= '::';
             
$f .= $funcname[$a];
            }
         
$f .= '($value, $key' . (!empty($userdata) ? ', $userdata' : '') . ');';
          eval(
$f);
        } else {
          if(!empty(
$userdata))
           
$funcname($value, $key, $userdata);
          else
           
$funcname($value, $key);
        }

        if(
$value != $saved_value)
         
$input[$key] = $value;
      }
    }
    return
true;
  } else {
   
array_walk_recursive($input, $funcname, $userdata);
  }
}

function
kv_addslashes(&$v, $k) {
 
$v = addslashes($v);
}
}
?>

Usage:
<?php
$arr
= array(
 
'a' => '"Hello World"',
 
'b' => "'Hello World'",
 
'c' => "Hello 'Worl\"d",
 
'd' => array(
   
'A' => 'H"e"l"l"o" "W"o"r"l"d'
   
)
  );

$class = new A_Class();
$class->array_walk_recursive($arr, array(&$class, 'kv_addslashes'));
print_r($arr);
?>
omega13a at sbcglobal dot net 21-Dec-2005 04:21
This is a peice of code I wrote that appears to create this function for PHP 4.

<?php
if (!function_exists('array_walk_recursive'))
{
    function
array_walk_recursive(&$input, $funcname, $userdata = "")
    {
        if (!
is_callable($funcname))
        {
            return
false;
        }
       
        if (!
is_array($input))
        {
            return
false;
        }
       
        foreach (
$input AS $key => $value)
        {
            if (
is_array($input[$key]))
            {
               
array_walk_recursive($input[$key], $funcname, $userdata);
            }
            else
            {
               
$saved_value = $value;
                if (!empty(
$userdata))
                {
                   
$funcname($value, $key, $userdata);
                }
                else
                {
                   
$funcname($value, $key);
                }
               
                if (
$value != $saved_value)
                {
                   
$input[$key] = $value;
                }
            }
        }
        return
true;
    }
}
?>

Please note it is a conditionaly set function and will have to be put before any call to it.

If there is anything wrong with it, please email me.
PHP8中文手册 站长在线 整理 版权归PHP文档组所有