从数组到 JSON
From array to JSON
我正在对文章进行解析,我需要将所有解析的数据放入 josn 中。我试图将它们放入数组,然后将其转换为 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);
}
尝试在 $arr
中删除一个 array
使用下面一个。
<?
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);
我正在对文章进行解析,我需要将所有解析的数据放入 josn 中。我试图将它们放入数组,然后将其转换为 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);
}
尝试在 $arr
array
使用下面一个。
<?
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);