我试图访问多维数组,并附加到其子键,但很难想出一个函数来执行此操作。
目前我的数组看起来像这样,键是文件夹的parent_id。
array (size=3)
0 =>
object(stdClass)[25]
public 'id' => string '18' (length=2)
18 =>
array (size=3)
19 =>
object(stdClass)[28]
public 'id' => string '19' (length=2)
20 =>
object(stdClass)[29]
public 'id' => string '20' (length=2)
21 =>
object(stdClass)[30]
public 'id' => string '21' (length=2)
19 =>
array (size=1)
24 =>
object(stdClass)[31]
public 'id' => string '24' (length=2)
我尝试过的事情:
function getChildren($folder_id)
{
$folder_cursor = $this->db->get_where("folder", array("id" => $folder_id));
if ($folder_cursor->num_rows() > 0) {
$row = $folder_cursor->row();
array_push($this->temp, $row);
$this->recursiveGetChildren($row->id);
}
}
function recursiveGetChildren($parent_id)
{
$q = $this->db->get_where("folder", array("parent" => $parent_id));
if ($q->num_rows() > 0) {
$this->temp[$parent_id] = array();
foreach($q->result() as $q) {
$this->temp[$parent_id][$q->id] = $q;
$this->recursiveGetChildren($q->id);
}
}
}
我希望数组看起来像这样:
array (size=3)
0 =>
object(stdClass)[25]
public 'id' => string '18' (length=2)
18 =>
array (size=3)
19 =>
array (size=2)
0=>
object(stdClass)[28]
public 'id' => string '19' (length=2)
24 =>
object(stdClass)[31]
public 'id' => string '24' (length=2)
20 =>
object(stdClass)[29]
public 'id' => string '20' (length=2)
21 =>
object(stdClass)[30]
public 'id' => string '21' (length=2)
为了清晰而编辑。
答案 0 :(得分:1)
您的结构如下:
array(
"child_id" => array(
"sub_child_id" => array();
),
"child_id_2" => array(
"sub_child_id_2" => array(
sub_sub_child_id => array();
);
),
);
表示以下内容:
$array[ $a[ $aa[ ] ], $b[ $bb[ $bbb[ ] ] ] ]
然后,对于每个数组来说,元素本身就是一个数组数组。第n个深度
<?php
$sweet = array('a' => 'apple', 'b' => 'banana');
$fruits = array('sweet' => $sweet, 'sour' => 'lemon');
function test_print($item, $key)
{
echo "$key holds $item\n";
}
array_walk_recursive($fruits, 'test_print');
?>
<强>实施强>