Php 使用简单的HTML DOM解析器来解析JSON?

Php 使用简单的HTML DOM解析器来解析JSON?,php,json,html-parsing,Php,Json,Html Parsing,我正试图将一个被刮掉的网站的每个元素分组,将其转换为一个json元素,但它似乎不起作用 <?php // Include the php dom parser include_once 'simple_html_dom.php'; header('Content-type: application/json'); // Create DOM from URL or file $html = file_get_html('urlhere'); foreach($html-&

我正试图将一个被刮掉的网站的每个元素分组,将其转换为一个json元素,但它似乎不起作用

<?php

// Include the php dom parser    
include_once 'simple_html_dom.php';

header('Content-type: application/json');

// Create DOM from URL or file

$html = file_get_html('urlhere');

foreach($html->find('hr ul') as $ul)
{
    foreach($ul->find('div.product') as $li) 
    $data[$count]['products'][]['li']= $li->innertext;
    $count++;
}
echo json_encode($data);

?> 
如何去除所有冗余信息,并命名重新格式化的json中的每个元素


谢谢

您只需在内部循环中扩展逻辑:

foreach($html->find('hr ul') as $ul)
{
    foreach($ul->find('div.product') as $li) {
        $product = array();

        $product['link'] = $li->find('a.th')[0]->href;
        $product['name'] = trim($li->find('div.name a')[0]->innertext);
        $product['subtitle'] = trim($li->find('div.subtitle')[0]->innertext);
        $product['image'] = explode("'", $li->find('div')[0]->style)[1];

        $data[$count]['products'][] = $product;
    }

}
echo json_encode($data);

我可以问一下如何删除“产品”前面的“{”“:{”:[{“链接”:…?只需将
$data[$count]['products'][]=$product;
更改为
$data['products'][]=$product;
您是最好的。谢谢您的帮助!:)
{"products":[{
"link":"/products/56942-haters-crewneck-sweatshirt",
"image":"http://s0.com/images/15814/v600_B_AltApparel_Crew.png",
"name":"Haters Crewneck Sweatshirt",
 "subtitle":"60.00"}
]}
foreach($html->find('hr ul') as $ul)
{
    foreach($ul->find('div.product') as $li) {
        $product = array();

        $product['link'] = $li->find('a.th')[0]->href;
        $product['name'] = trim($li->find('div.name a')[0]->innertext);
        $product['subtitle'] = trim($li->find('div.subtitle')[0]->innertext);
        $product['image'] = explode("'", $li->find('div')[0]->style)[1];

        $data[$count]['products'][] = $product;
    }

}
echo json_encode($data);