note 101371 added to function.array-walk-recursive

From: Date: Mon, 13 Dec 2010 05:10:10 +0000
Subject: note 101371 added to function.array-walk-recursive
Groups: php.notes 
Request: Send a blank email to php-notes+get-174839@lists.php.net to get a copy of this message
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 ) */ ?> ---- Server IP: 69.147.83.197 Probable Submitter: 150.203.57.46 ---- Manual Page -- http://www.php.net/manual/en/function.array-walk-recursive.php Edit -- https://master.php.net/note/edit/101371 Del: integrated -- https://master.php.net/note/delete/101371/integrated Del: useless -- https://master.php.net/note/delete/101371/useless Del: bad code -- https://master.php.net/note/delete/101371/bad+code Del: spam -- https://master.php.net/note/delete/101371/spam Del: non-english -- https://master.php.net/note/delete/101371/non-english Del: in docs -- https://master.php.net/note/delete/101371/in+docs Del: other reasons-- https://master.php.net/note/delete/101371 Reject -- https://master.php.net/note/reject/101371 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#174839) next »