猿问

从数组到 JSON

我正在解析文章,我需要将所有解析的数据放在 josn.xml 中。我尝试将它们放入数组,然后将其转换为 JSON,但我遇到了一些麻烦。我得到这样的JSON:


[{"title":"title1"}][{"title":"title2"}][{"title":"title3"}]


但我想要这样:


[{"title":"title1"},{"title":"title2"},{"title":"title3"}]


我怎么能做到这一点?


<?


foreach ($content_prev as $el) {

    $pq = pq($el);

    $date = $pq->find('time')->html();

    $title = $pq->find('h3 a')->html();

    $link = $pq->find('h3 a')->attr('href');


    $data_link = file_get_contents($link);

    $document_с = phpQuery::newDocument($data_link);

    $content = $document_с->find('.td-post-content');


    $arr = array (

        array( 

            "title" => $title

        ), 

    ); 


    echo json_encode($arr, JSON_UNESCAPED_UNICODE);

}


慕慕森
浏览 95回答 1
1回答

婷婷同学_

尝试删除array一个$arr使用以下一种。<?foreach ($content_prev as $el) {&nbsp; &nbsp; $pq = pq($el);&nbsp; &nbsp; $date = $pq->find('time')->html();&nbsp; &nbsp; $title = $pq->find('h3 a')->html();&nbsp; &nbsp; $link = $pq->find('h3 a')->attr('href');&nbsp; &nbsp; $data_link = file_get_contents($link);&nbsp; &nbsp; $document_с = phpQuery::newDocument($data_link);&nbsp; &nbsp; $content = $document_с->find('.td-post-content');&nbsp; &nbsp; $arr[] = array (&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "title" => $title&nbsp; &nbsp; &nbsp; &nbsp; );}echo json_encode($arr, JSON_UNESCAPED_UNICODE);
随时随地看视频慕课网APP
我要回答