PHP complex multidimensional array iteration -


i need iterate through multidimensional array. need complex: iteration must go levels - @ first elements of 1 level, after them elements of 2 level, after them elements of 3 level, etc.

i can write myself if want to, need make sure i'm not reinventing wheel. there ready implementation of in php?

update

what code should provide? it's theoretical issue. can provide array.. :

array (     [lev1_1] => array         (             [id] => 3547             [children] => array                 (                     [lev2_1] => array                         (                             [id] => 3550                         )                      [lev2_2] => array                         (                             [id] => 3551                         )                 )         )     [lev1_2] => array         (             [id] => 3547             [children] => array                 (                     [lev2_3] => array                         (                             [id] => 3550                             [children] => array                                 (                                     [lev3_1] => array                                         (                                             [id] => 3550                                         )                                      [lev3_2] => array                                         (                                             [id] => 3551                                         )                                 )                         )                      [lev2_4] => array                         (                             [id] => 3551                         )                 )         ) 

i need iterate through lev1_x, lev2_x, lev3_x

well, here's dumb way values level. please note you'll need tweak code according use.

function array_values_by_level($array, $level = 0, &$values = null) {     if ($values === null)         $values = array();     if (!isset($values[$level]))         $values[$level] = array();     foreach ($array $v) {         if (is_array($v))             array_values_by_level($v, $level + 1, &$values);         else             $values[$level][] = $v;     }      return $values; } 

test case:

$big_array = array(     '1',     '2',     array(         '2.1',         '2.2',         array(             '2.2.1',             '2.2.2',             array(                 '2.2.2.1'             )         ),         '2.3',         array(             '2.3.1',             array(                 '2.3.1.1',                 '2.3.1.2'             )         )     ) );  foreach (array_values_by_level($big_array) $k => $v)     printf("[%d] %s \n", $k, implode(', ', $v));  // [0] 1, 2 // [1] 2.1, 2.2, 2.3 // [2] 2.2.1, 2.2.2, 2.3.1 // [3] 2.2.2.1, 2.3.1.1, 2.3.1.2 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -