recursion - Recursive get path from multidimensional php array -


i have array:

$adm_menu_old = array (     array(          'id' => 1,         'name' => 'test1',     ),      array(          'id' => 3,         'name' => 'test3',         'childrens' => array(             array(                 'id' => 31,                 'name' => 'test31',             ),             array(                 'id' => 32,                 'name' => 'test32',                 'childrens' => array(                      array(                         'id' => 321,                         'name' => 'test321',                      ),             ),         )     ),      array(          'id' => 4,         'name' => 'test4',     ),  ); 

say know id value. need path parents of id. example need path element: id=321 need array key name values:

array('test3','test32','test321') 

how should recursive function?

try function:

function getnames($id, $arr) {   $result = array();     foreach($arr $key => $val) {       if(is_array($val)) {         if($val["id"] == $id) {           $result[] = $val["name"];         } elseif(!empty($val["childrens"]) && is_array($val["childrens"])) {           $sub_res = getnames($id, $val["childrens"]);           if(count($sub_res) > 0) {           $result[] = $val["name"];           $result = array_merge($result, $sub_res);         }       }     }   }   return $result; } 

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 -