gpt4 book ai didi

php - 如何循环遍历 N 级子子数组 - PHP

转载 作者:行者123 更新时间:2023-12-02 20:16:19 25 4
gpt4 key购买 nike

我有类别和子类别。我有这种类型的数组。

任何人都可以帮助我如何循环遍历n层。

Array
(
[0] => Array
(
[question_title] => hello.
[id] => 1
[step] => 1
[Children] => Array
(
)

)

[1] => Array
(
[question_title] => bye
[id] => 2
[step] => 1
[Children] => Array
(
)

)

[2] => Array
(
[question_title] => gtg
[id] => 3
[step] => 1
[Children] => Array
(
)

)

[3] => Array
(
[question_title] => cya
[id] => 4
[step] => 1
[Children] => Array
(
)

)

[4] => Array
(
[question_title] => not sure
[id] => 5
[step] => 1
[Children] => Array
(
)

)

[5] => Array
(
[question_title] => will see
[id] => 6
[step] => 1
[Children] => Array
(
)

)

[6] => Array
(
[question_title] =>idk
[id] => 7
[step] => 2
[Children] => Array
(
)

)

[7] => Array
(
[question_title] => mayebthis
[id] => 8
[step] => 2
[Children] => Array
(
)

)

[8] => Array
(
[question_title] => maybe this one as well
[id] => 9
[step] => 2
[Children] => Array
(
[0] => Array
(
[question_title] => maybe this one too
[id] => 10
[step] => 2
[Children] => Array
(
)

)

)

)

[9] => Array
(
[question_title] => and this
[id] => 11
[step] => 2
[Children] => Array
(
[0] => Array
(
[question_title] => or this
[id] => 12
[step] => 2
[Children] => Array
(
)

)

[1] => Array
(
[question_title] =>or here too
[id] => 13
[step] => 2
[Children] => Array
(
[0] => Array
(
[question_title] => wait i am also here
[id] => 14
[step] => 2
[Children] => Array
(
)

)

[1] => Array
(
[question_title] => me as well
[id] => 15
[step] => 2
[Children] => Array
(
)

)

[2] => Array
(
[question_title] => me me scream
[id] => 16
[step] => 2
[Children] => Array
(
)

)

)

)

)

)

[10] => Array
(
[question_title] => outside here
[id] => 17
[step] => 2
[Children] => Array
(
)

)

[11] => Array
(
[question_title] => outisde here too
[id] => 18
[step] => 2
[Children] => Array
(
)

)

)

我正在循环主数组,但我不知道如何循环子数组,因为也许也可以有许多子子数组。

{foreach $question_master_array as $key => $val}
{if $question_master_array[$key]['step'] == 2}
<li><input type="checkbox" name="step1_question_{$question_master_array[$key]['question_id']}">{$question_master_array[$key]['question_title']}</li>
{if $question_master_array[$key]['Children']|@count gt 0}
... some stuff here ...
{/if}
{/if}
{/foreach}

因为我不能通过仅将 key 应用到 Children 数组来使其静态,因为它可以有 n 级。嗯,这是我的 tpl 文件,因此我无法编辑以这种数据格式引入的查询。

最佳答案

您需要递归地遍历数组。

示例:

function recurSearch($array, $depth = 0)
{
echo ('Current depth : ' . $depth);
foreach ($array as $item)
{
echo ('id : ' . $item['id']);
echo ('question title : ' . $item['question_title']);
echo ('step : ' . $item['step']);

if (count($item['Children']) > 0)
{
echo 'child : ';
echo '{';
/*
* If $item['Children'] isn't empty,
* the function will be called again
* using $item['Children'] as main array
* That function will keep being called until $item['Children'] is empty
*/
recurSearch($item['Children'], $depth + 1);
echo '}';
}
else
{
echo ('child : no child');
}
}
}

关于php - 如何循环遍历 N 级子子数组 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52312048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com