From array to JSON

56 views Asked by At

I am making parser of articles and I need to put all parsed data in josn. I tried to put them to array and then transform it in JSON, but I have some troubles. I get JSON like this:

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

But I want like this:

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

How I can do this?

<?

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);
}
1

There are 1 answers

4
HP371 On BEST ANSWER

Try to remove one array in $arr

Use below one.

<?
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 ( 
        "title" => $title
        );
}
echo json_encode($arr, JSON_UNESCAPED_UNICODE);