Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过PHP对齐帖子下的评论_Php - Fatal编程技术网

如何通过PHP对齐帖子下的评论

如何通过PHP对齐帖子下的评论,php,Php,我有两个json记录,我可以成功地分别显示它的记录 这是我的问题。我想根据第一条记录的postid附加并显示第二条记录的注释 也就是说,我想根据posted将所有评论放在它的post下 <?php // first record $output =' {"results":[ {"postid":101, "post":"my first post"}, {"postid":102, &q

我有两个json记录,我可以成功地分别显示它的记录

这是我的问题。我想根据第一条记录的postid附加并显示第二条记录的注释

也就是说,我想根据posted将所有评论放在它的post下

<?php

// first record
$output ='
{"results":[
{"postid":101, "post":"my first post"},
{"postid":102, "post":"my second post"},
{"postid":103, "post":"my third post"}]
}
';


// second record
$output2 ='
{"results":[
{"postid":101,"comment":"my first comment"},
{"postid":102, "comment":"my second comment"},
{"postid":103,"comment":"my third comment"}
]
}
';


$json = json_decode($output, true);
foreach($json["results"] as $res){

            
echo $id = $res['postid'];
echo "<br><br>";

echo $post = $res['post'];
echo "<br><br>";
  
}




$json2 = json_decode($output2, true);
foreach($json2["results"] as $res1){

            
echo $id = $res1['postid'];
echo "<br><br>";

echo $comment = $res1['comment'];
echo "<br><br>";

  
}


?>

在两个数组中循环,找到匹配的
postd
,如果它们匹配,则在该键处将第一个数组的
post
添加到第二个数组中


您需要添加第二个
json\u decode
,并在内部循环中从中获取注释

<?php

// first record
$output ='
{"results":[
{"postid":101, "post":"my first post"},
{"postid":102, "post":"my second post"},
{"postid":103, "post":"my third post"}]
}
';


// second record
$output2 ='
{"results":[
{"postid":101,"comment":"my first comment"},
{"postid":102, "comment":"my second comment"},
{"postid":103,"comment":"my third comment"}
]
}
';


$json = json_decode($output, true);
$json2 = json_decode($output2, true);

foreach($json["results"] as $key => $res){


echo $id = $res['postid'];
echo "<br><br>";

echo $post = $res['post'];
echo "<br><br>";
echo $json2['results'][$key]['comment'];
echo "<br><br>";

}

基本上,在每次打印文章标题和id的迭代中,都需要循环
$output2
中包含的整个注释记录,如果找到匹配的
id
,则需要打印相关注释。它是次优的,但逻辑上是这样的:

<?php   
// first record
$posts ='{"results":[
                        {"postid":101, "post":"my first post"},
                        {"postid":102, "post":"my second post"},
                        {"postid":103, "post":"my third post"}]}';

// second record
$comments='{"results":[
                        {"postid":101,"comment":"my first comment"},
                        {"postid":102, "comment":"my second comment"},
                        {"postid":103,"comment":"my third comment"}]}';

// decode the posts
$json_posts = json_decode($posts, true);

// decode the comments
$json_comments = json_decode($comments, true);

// loop through the posts
foreach($json_posts["results"] as $post){
    
    // renamed variables to avoid naming clash
    echo $_id = $post['postid'], '<br><br>', "\n";
    echo $_post = $post['post'], '<br><br>', "\n";

    // loop through the comments
    foreach($json_comments ["results"] as $comment) {
        if ($comment["postid"] == $id) {
            echo $_comment = $comment["comment"], '<br><br>', "\n";
        }
    }
}

注释数组有可能包含同一
postid
的多个条目,在这种情况下,我认为这是行不通的。
<?php

// first record
$output ='
{"results":[
{"postid":101, "post":"my first post"},
{"postid":102, "post":"my second post"},
{"postid":103, "post":"my third post"}]
}
';


// second record
$output2 ='
{"results":[
{"postid":101,"comment":"my first comment"},
{"postid":102, "comment":"my second comment"},
{"postid":103,"comment":"my third comment"}
]
}
';


$json = json_decode($output, true);
$json2 = json_decode($output2, true);

foreach($json["results"] as $key => $res){


echo $id = $res['postid'];
echo "<br><br>";

echo $post = $res['post'];
echo "<br><br>";
echo $json2['results'][$key]['comment'];
echo "<br><br>";

}
101

my first post

my first comment

102

my second post

my second comment

103

my third post

my third comment
<?php   
// first record
$posts ='{"results":[
                        {"postid":101, "post":"my first post"},
                        {"postid":102, "post":"my second post"},
                        {"postid":103, "post":"my third post"}]}';

// second record
$comments='{"results":[
                        {"postid":101,"comment":"my first comment"},
                        {"postid":102, "comment":"my second comment"},
                        {"postid":103,"comment":"my third comment"}]}';

// decode the posts
$json_posts = json_decode($posts, true);

// decode the comments
$json_comments = json_decode($comments, true);

// loop through the posts
foreach($json_posts["results"] as $post){
    
    // renamed variables to avoid naming clash
    echo $_id = $post['postid'], '<br><br>', "\n";
    echo $_post = $post['post'], '<br><br>', "\n";

    // loop through the comments
    foreach($json_comments ["results"] as $comment) {
        if ($comment["postid"] == $id) {
            echo $_comment = $comment["comment"], '<br><br>', "\n";
        }
    }
}