Quantcast
Channel: Get path from adjacency list data - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by Narendrasingh Sisodia for Get path from adjacency list data

$
0
0
function getpath($id, $arr, $level = 0) {    $result = array();    foreach($arr as $key => $value){        if($id == $value['id']){            $result[] = $value['name'];            $id = $value['parent'];            if($id != 0){              $result = array_merge($result, getpath($id, $arr, $level+1));            }else{                break;            }        }    }    return $level ? $result : implode('->',array_reverse($result));}echo getpath(13,$arr);

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>