note 101371 deleted from function.array-walk-recursive by danbrown
| From: | danbrown@php.net | Date: | Mon, 13 Dec 2010 16:38:56 +0000 |
| Subject: | note 101371 deleted from function.array-walk-recursive by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174883@lists.php.net to get a copy of this message | ||
Note Submitter: zhang dot taihao at gmail dot com
----
The earlier example using an anonymous function requires passing $output by reference, which will
trigger a "Call-time pass-by-reference has been deprecated" warning. The workaround is to
pass an array by value, which contains a reference to the working data you wish to modify.
<?php
// The array to walk through
$data = array(
'a' => array(
'b' => 'b',
'c' => 'c',
),
'd' => 'd',
);
$output = array();
// $userdata contains a reference to $output
array_walk_recursive($data, create_function('$val, $key, $obj',
'array_push($obj["output"], $val);'), array('output' =>
&$output));
print_r($output);
/*
Array
(
[0] => b
[1] => c
[2] => d
)
*/
?>
Alternatively, to retain the keys, slightly modify the anonymous function:
<?php
// Reset output
$output = array();
array_walk_recursive($data, create_function('$val, $key, $obj',
'$obj["output"][$key] = $val;'), array('output' => &$output));
print_r($output);
/*
Array
(
[b] => b
[c] => c
[d] => d
)
*/
?>