3

after fetching data from database and converting it into tree structure my array looks like this.

array(1) {  
  [6]=>  
  array(1) {  
    ["sub_id"]=>  
    array(3) {  
      [15]=>  
      array(1) {  
        ["sub_id"]=>  
        array(0) {  
        }    
      }  
      [16]=>  
      array(1) {  
        ["sub_id"]=>  
        array(0) {  
        }  
      }  
      [21]=>  
      array(1) {  
        ["sub_id"]=>  
        array(0) {  
        }  
      }    
    }    
  }  
}

but i want flat array of only keys array{6,15,16,21}

Pratig
  • 148
  • 3
  • 12

1 Answers1

2

I have created a function for your output. Please try this.

$result = get_elements($array);

function get_elements($array) {
    $result = array();
    foreach($array as $key => $row) {
        $result[] = $key;
        if(count($row['sub_id']) > 0) {
            $result = array_merge($result,get_elements($row['sub_id']));
        }
    }
    return $result;
}
noufalcep
  • 3,446
  • 15
  • 33
  • 51