note 114584 added to function.array-keys

From: Date: Sun, 09 Mar 2014 03:41:52 +0000
Subject: note 114584 added to function.array-keys
Groups: php.notes 
Request: Send a blank email to php-notes+get-198839@lists.php.net to get a copy of this message
There's a lot of multidimensional array_keys function out there, but each of them only merges all the keys in one flat array. Here's a way to find all the keys from a multidimensional array while keeping the array structure. An optional MAXIMUM DEPTH parameter can be set for testing purpose in case of very large arrays. NOTE: If the sub element isn't an array, it will be ignore. <?php function array_keys_recursive($myArray, $MAXDEPTH = INF, $depth = 0, $arrayKeys = array()){ if($depth < $MAXDEPTH){ $depth++; $keys = array_keys($myArray); foreach($keys as $key){ if(is_array($myArray[$key])){ $arrayKeys[$key] = array_keys_recursive($myArray[$key], $MAXDEPTH, $depth); } } } return $arrayKeys; } ?> EXAMPLE: input: array( 'Player' => array( 'id' => '4', 'state' => 'active', ), 'LevelSimulation' => array( 'id' => '1', 'simulation_id' => '1', 'level_id' => '1', 'Level' => array( 'id' => '1', 'city_id' => '8', 'City' => array( 'id' => '8', 'class' => 'home', ) ) ), 'User' => array( 'id' => '48', 'gender' => 'M', 'group' => 'user', 'username' => 'Hello' ) ) output: array( 'Player' => array(), 'LevelSimulation' => array( 'Level' => array( 'City' => array() ) ), 'User' => array() ) ---- Server IP: 216.235.13.26 Probable Submitter: 132.210.186.45 ---- Manual Page -- http://php.net/manual/en/function.array-keys.php Edit -- https://master.php.net/note/edit/114584 Del: integrated -- https://master.php.net/note/delete/114584/integrated Del: useless -- https://master.php.net/note/delete/114584/useless Del: bad code -- https://master.php.net/note/delete/114584/bad+code Del: spam -- https://master.php.net/note/delete/114584/spam Del: non-english -- https://master.php.net/note/delete/114584/non-english Del: in docs -- https://master.php.net/note/delete/114584/in+docs Del: other reasons-- https://master.php.net/note/delete/114584 Reject -- https://master.php.net/note/reject/114584 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198839) next »