note 103901 added to language.types.array

From: Date: Tue, 10 May 2011 12:15:59 +0000
Subject: note 103901 added to language.types.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-179576@lists.php.net to get a copy of this message
This is another way to get value from a multidimensional array, but for versions of php >= 5.3.x <?php function array_value_recursive($key, array $arr){ $val = null; array_walk_recursive($arr, function($v, $k) use($key, &$val){ $val = $k == $key ? $v : (!is_null($val) ? $val : false); }); return $val; } $arr = array( 'foo' => 'foo', 'bar' => array( 'baz' => 'baz', 'candy' => 'candy', 'vegetable' => array( 'carrot' => 'carrot', ) ), 'fruits' => 'fruits', ); var_dump(array_value_recursive('carrot', $arr)); // string(6) "carrot" var_dump(array_value_recursive('apple', $arr)); // bool(false) var_dump(array_value_recursive('baz', $arr)); // string(3) "baz" var_dump(array_value_recursive('candy', $arr)); // string(5) "candy" var_dump(array_value_recursive('pear', $arr)); // bool(false) ?> ---- Server IP: 69.147.83.197 Probable Submitter: 64.237.177.99 ---- Manual Page -- http://www.php.net/manual/en/language.types.array.php Edit -- https://master.php.net/note/edit/103901 Del: integrated -- https://master.php.net/note/delete/103901/integrated Del: useless -- https://master.php.net/note/delete/103901/useless Del: bad code -- https://master.php.net/note/delete/103901/bad+code Del: spam -- https://master.php.net/note/delete/103901/spam Del: non-english -- https://master.php.net/note/delete/103901/non-english Del: in docs -- https://master.php.net/note/delete/103901/in+docs Del: other reasons-- https://master.php.net/note/delete/103901 Reject -- https://master.php.net/note/reject/103901 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#179576) next »