如何为JSON数组中可能有子数组的所有子数组动态重复(PHP)
我正在尝试编写一个网站,该网站接受Reddit页面的评论并显示它们。但是,这些评论有回复,我也想展示它们,但是一个评论可以没有回复,一个或多个回复,这些回复可以有回复。有没有一种方法可以对所有的回复重复相同的代码,但有细微的差异(缩进)?我正在使用reddit json特性,并从以下内容获取json:。 我有:如何为JSON数组中可能有子数组的所有子数组动态重复(PHP),php,recursion,multidimensional-array,Php,Recursion,Multidimensional Array,我正在尝试编写一个网站,该网站接受Reddit页面的评论并显示它们。但是,这些评论有回复,我也想展示它们,但是一个评论可以没有回复,一个或多个回复,这些回复可以有回复。有没有一种方法可以对所有的回复重复相同的代码,但有细微的差异(缩进)?我正在使用reddit json特性,并从以下内容获取json:。 我有: $url=('https://www.reddit.com/r/pcmasterrace/comments/dln0o3/foldinghome_and_pcmr_team_up_use
$url=('https://www.reddit.com/r/pcmasterrace/comments/dln0o3/foldinghome_and_pcmr_team_up_use_your_pc_to_help/.json');
$json=文件获取内容($url);
$obj=json_decode($json,true);
$comment_array=array_slice($obj[1]['data']['children'],0,50);
回声';
foreach($c作为注释数组){
echo“(“$c['data']['author']”)、“$c['data']['score']”得分
“$c['data']['body']”。””;
如果(!($c['data']['replays']==“”){
$r1_数组=$c['data']['replays']['data']['children'];
foreach($r1\u数组作为$r1){
echo“(“$r1['data']['author']”“)”)“$r1['data']['score']”得分
“$r1['data']['body']”。””;
如果(!($r1['data']['replies']==“”){
$r2_数组=$r1['data']['repress']['data']['children'];
foreach($r2\u数组作为$r2){
echo“(“$r2['data']['author']”“)”)“$r1['data']['score']”得分
“$r2['data']['body']”。””;
}
}
}
}
}
}
这将产生所需的结果,包括回复回复等。然而,它有点凌乱,如果有一个很长的回复链,它就不会抓住它。有没有办法让它以某种方式重复,或者我应该复制并粘贴多次?
非常感谢 我想你在寻找一个叫做 其基本思想是,函数将根据需要多次调用自身(而不是使用固定数量的循环) 大概是这样的:
<?php
function output($data, $level = 0) {
$spaces = str_repeat(' ', $level);
foreach ($data as $post) {
echo "<p>(".$spaces.$post['data']['author'].") ". $post['data']['score'] . " Points<br>".$post['data']['body']."</p>\r\n";
if ($post['data']['replies']) {
// Notice that we are calling the function again, this time increasing the level
// This is the "recursive" part of the function
output($post['data']['replies']['data']['children'], $level + 1);
}
}
}
$url = ('https://www.reddit.com/r/pcmasterrace/comments/dln0o3/foldinghome_and_pcmr_team_up_use_your_pc_to_help/.json');
$json = file_get_contents($url);
$data = json_decode($json, true);
$comment_array = array_slice($data[1]['data']['children'], 0, 50);
output($comment_array);
?>
好的,听着,让我们把问题精简一下,尽量让你的问题清晰、最少、可验证。删除代码段中的前三行,因为您在接收文件内容时没有任何问题。(当您的输入实际上是一个数组时,我们不要将其称为
$obj
)向我们展示一个足够大的示例输入($array
)--它需要有足够的回复以及其他任何内容,以便数据是真实的,但我们不想要一个包含1000个元素的数组。您已经向我们展示了您的代码,这非常棒。最后,向我们展示您当前的输出和所需的输出。请编辑您的问题。
<?php
function output($data, $level = 0) {
$spaces = str_repeat(' ', $level);
foreach ($data as $post) {
echo "<p>(".$spaces.$post['data']['author'].") ". $post['data']['score'] . " Points<br>".$post['data']['body']."</p>\r\n";
if ($post['data']['replies']) {
// Notice that we are calling the function again, this time increasing the level
// This is the "recursive" part of the function
output($post['data']['replies']['data']['children'], $level + 1);
}
}
}
$url = ('https://www.reddit.com/r/pcmasterrace/comments/dln0o3/foldinghome_and_pcmr_team_up_use_your_pc_to_help/.json');
$json = file_get_contents($url);
$data = json_decode($json, true);
$comment_array = array_slice($data[1]['data']['children'], 0, 50);
output($comment_array);
?>