Joy Amar
Joy Amar

Reputation: 3

Recursively get children of parent id in array

I have this array

Array([23] => Array([21] => Array([1] => Array([] => Array()))))

I need to be

$result = array(23,32,1,0);

Please someone help me.

Upvotes: 0

Views: 51

Answers (1)

vivek modi
vivek modi

Reputation: 812

<?php 
$data=array(23=>array(21=>array(1=>array(''=>array()))));
// print_r($data);
$arr=array();
a:
foreach ($data as $key => $value) {

    if(is_array($data[$key])){
        $data=$data[$key];
        // continue;
    }

    if($key==''){
        $key=0; 
    }
    $arr[]=$key;
    goto a;

    // print_r($data);
}
print_r($arr);
?>

Upvotes: 1

Related Questions