sözlükler dizisi CodeIgniter'daki özyinelemeli fonksiyonun nesnelerin dizi artığını

0 Cevap php

I've noticed some very strange behaviour today in Code Igniter. I have this locations table in my database which has the columns id,name,id_parent,level (it's a tree of locations) and i've tried to generate an array of locations sorted depth first. My approach was to build a recursive function in a helper file:

$CI = & get_instance();
$CI->load->database();

function dfs_locations($node, $level){
    global $CI;
    $result = array();
    $result[] = array('id'=>$node,'level'=>$level);
    $q = $CI->db->query(
        'select l1.id from locations l1, locations l2'.
        ' where l1.id_parent=l2.id and l2.id='.$node.' order by l1.id'
    );
    foreach ($q->result() as $row){
        $result[] = json_decode(dfs_locations($row->id,$level+1));
    }
    return json_encode($result);
}

Sonuç biraz garip. Baskı json_decode(dfs_locations($root_id,1)) verimleri "

Array
(
[0] => Array
(
[0] => stdClass Object
(
[id] => 1
[nivel] => 1
)

[1] => Array
(
[0] => stdClass Object
(
[id] => 2
[nivel] => 2
)

and so on.Without json encoding/decoding i get even stranger results and only the root gets printed. Any help is greatly appreciated. Thank you.

0 Cevap