/ / Json Відображається як окремий формат - php, масиви, json, wordpress

Json Відображається як індивідуальний формат - php, масиви, json, wordpress

Я успішно отримую коментарі до публікації Wordpressчерез код PHP. Тепер я хочу відобразити всі коментарі до публікації у форматі JSON. Я навів кілька прикладів, щоб завершити цей прогрес, але всі коментарі (формат JSON) відображаються окремо.

Перегляньте зразок результату:

[{
"author": "JimmiXzSq",
"comment": "test1"
}]

[{
"author": "MoseJackswka",
"comment": "test2"
}]

Але я хочу відображати правильний формат JSON

Мій код тренування:

foreach ( $comments as $comment ) :

$getauthor = $comment->comment_author ;
$getcontent =  $comment->comment_content;
$test1=  array("author" => $getauthor, "comment" => $getcontent);

$displaycomments = json_encode(array(($test1)),true);
echo $displaycomments;
endforeach;

Відповіді:

0 для відповіді № 1

Якщо ви хочете вставити всі свої коментарі в один json:

$comments = array();

foreach ( $comments as $comment ) :
$getauthor = $comment->comment_author ;
$getcontent =  $comment->comment_content;
$tmp=  array("author" => $getauthor, "comment" => $getcontent);

$comment[] = $tmp;
endforeach;

$displaycomments = json_encode($comments);
echo $displaycomments ;

дам

{
{
"author": "JimmiXzSq",
"comment": "test1"
}
,
{
"author": "MoseJackswka",
"comment": "test2"
}
}