私は構造parent => childのようなツリーを持つ多次元配列を持っています。私は最後の配列ノードに子を追加したいが、私はどのように把握することはできません。下に私の配列とコードがありますが、私はちょうど詰まっています。多次元配列に子を追加する
Array
(
[id] => 154
[text] => root
[parent_id] =>
[children] => Array
(
[155] => Array
(
[id] => 155
[text] => DE
[parent_id] => 154
[children] => Array
(
[157] => Array
(
[id] => 157
[text] => Mülheim
[parent_id] => 155
[children] => Array
(
)
)
[158] => Array
(
[id] => 158
[text] => Heißen
[parent_id] => 155
[children] => Array
(
)
)
[159] => Array
(
[id] => 159
[text] => Essen
[parent_id] => 155
[children] => Array
(
)
)
)
)
[156] => Array
(
[id] => 156
[text] => RO
[parent_id] => 154
[children] => Array
(
[160] => Array
(
[id] => 160
[text] => Alba Iulia
[parent_id] => 156
[children] => Array
(
)
)
[161] => Array
(
[id] => 161
[text] => Sibiu
[parent_id] => 156
[children] => Array
(
)
)
)
)
)
)
私の再帰的な横断関数です。どんな助けもありがとうございます。ありがとう!
function traverseArray($array, &$in_arr = array())
{
foreach($array as $k=>$v)
{
if($k == 'children' && empty($v)){
// here i am querying children data for the end node that has ['children'] => array().
// I did some tests and it seems that no matter what i do here does not affect the $in_array variable at all
$data = Location::all(array('conditions'=>array("parent_id=?", $array['id'])));
// here I try to assign the new childrens to $in_array
foreach ($data as $k=>$v){
$in_arr['children'] = array('href'=>$v->id,'text'=>$v->name);
}
}
else{
$in_arr[] = $v;
}
if(is_array($v))
{
traverseArray($v);
}
}
return $in_arr;
}