我想在多维数组中达到10级。
这是我的代码,我得到无限的深度,但我想限制这10个级别。
function tree($parent){
global $db;
$sql = "SELECT * FROM clients WHERE parent_id = $parent ";
$users=$db->query($sql);
$return = array();
while($parent = $users->fetchObject()) {
$return[$parent->client_id] = $parent;
if(have_data($parent->client_id)){
$return[$parent->client_id]->subs = tree($parent->client_id);
}
}
return $return;
}
print_r(树(1,0));
输出为:
Array
(
[2] => stdClass Object
(
[client_id] => 2
[parent_id] => 1
[client_name] => TEST
[subs] => Array
(
[4] => stdClass Object
(
[client_id] => 4
[parent_id] => 2
[client_name] => TEST
[subs] => Array
(
[5] => stdClass Object
(
[client_id] => 5
[parent_id] => 4
[client_name] => ADAm
)
)
)
[6] => stdClass Object
(
[client_id] => 6
[parent_id] => 2
[client_name] => RAS
[subs] => Array
(
[7] => stdClass Object
(
[client_id] => 7
[parent_id] => 6
[client_name] => RAStest
)
)
)
)
)
[3] => stdClass Object
(
[client_id] => 3
[parent_id] => 1
[client_name] => ABC
)
)
如何将孩子限制在10级,希望得到专家老板的回应。
您可以使用RecursiveIteratorIterator API并使用setMaxDepth()函数,并可以通过此函数getMaxDepth()获得maxDepth
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($myArray));