Php 为什么不是';我的数组不能转换为XML吗?

Php 为什么不是';我的数组不能转换为XML吗?,php,xml,arrays,Php,Xml,Arrays,我有一个数组($result),我正试图将其转换并输出为XML,但我没有这样做 有什么想法可能是错误的吗 以下是: 下面是代码: function scrape($list_url, $shop_name, $photo_location, $photo_url_root, $product_location, $product_url_root, $was_price_location, $now_price_location, $gender, $country) { head

我有一个数组(
$result
),我正试图将其转换并输出为XML,但我没有这样做

有什么想法可能是错误的吗

以下是:


下面是代码:

function scrape($list_url, $shop_name, $photo_location, $photo_url_root, $product_location, $product_url_root, $was_price_location, $now_price_location, $gender, $country)
{

    header("Content-Type: application/xml; charset=UTF-8");

    $xml = new SimpleXMLElement('<rss/>');
    $xml->addAttribute("version", "2.0");
    $channel = $xml->addChild("channel");

    $html = file_get_contents($list_url);
    $doc = new DOMDocument();
    libxml_use_internal_errors(TRUE);

    if (!empty($html))
    {
        $doc->loadHTML($html);
        libxml_clear_errors(); // remove errors for yucky html
        $xpath = new DOMXPath($doc);
        /* FIND LINK TO PRODUCT PAGE */
        $products = array();
        $entries = array();
/* product_url */
        $row = $xpath->query($product_location);
        if ($row->length > 0)
        {
            foreach ($row as $location)
            {
                $product_url[] = $product_url_root . $location->getAttribute('href');
            }
        }
/* photo_url */
        $imgs = $xpath->query($photo_location);
        if ($imgs->length > 0)
        {
            foreach ($imgs as $img)
            {
                $photo_url[] = $photo_url_root . $img->getAttribute('src');
            }
        }
        $result = array();
        foreach ($product_url as $i => $val)
        {
            $result[] = array(
                'product_url' => $val,
                'shop_name' => $shop_name,
                'photo_url' => $photo_url[$i]
            );
        }

        //print_r($result);

        $item = $channel->addChild("item");
        $item->addChild("product_url", $result['product_url']);
        $item->addChild("shop_name", $result['shop_name']);
        $item->addChild("photo_url", $result['photo_url']);

    }
    echo $xml->asXML();
}
函数刮取($list\u url、$shop\u name、$photo\u location、$photo\u url\u root、$product\u location、$product\u url\u root、$was\u price\u location、$now\u price\u location、$gender、$country)
{
标题(“内容类型:application/xml;字符集=UTF-8”);
$xml=新的SimpleXMLElement(“”);
$xml->addAttribute(“版本”,“2.0”);
$channel=$xml->addChild(“channel”);
$html=文件内容($list\u url);
$doc=新的DOMDocument();
libxml\u使用\u内部错误(TRUE);
如果(!empty($html))
{
$doc->loadHTML($html);
libxml_clear_errors();//删除讨厌的html的错误
$xpath=新的DOMXPath($doc);
/*查找产品页面的链接*/
$products=array();
$entries=array();
/*产品url*/
$row=$xpath->query($product\u位置);
如果($row->length>0)
{
foreach($row作为$location)
{
$product\U url[]=$product\U url\U root.$location->getAttribute('href');
}
}
/*照片地址*/
$imgs=$xpath->query($photo\u位置);
如果($imgs->length>0)
{
foreach($imgs作为$img)
{
$photo_url[]=$photo_url_root.$img->getAttribute('src');
}
}
$result=array();
foreach($i=>$val的产品url)
{
$result[]=数组(
“产品url”=>$val,
'shop_name'=>$shop_name,
'photo_url'=>$photo_url[$i]
);
}
//打印(结果);
$item=$channel->addChild(“item”);
$item->addChild(“产品url”,$result['product\uURL']);
$item->addChild(“shop_name”,$result['shop_name']);
$item->addChild(“photo_url”,$result['photo_url']);
}
echo$xml->asXML();
}

将addchild移动到
foreach
循环中。我已将其移动到最后一个(第三个)foreach中,如果单击我的输出链接,您将看到输出。是的,因为
[]
的=>
$result[]=array(
在这种情况下应该是
$result=array(
。谢谢,那太好了。如果你想把它写下来作为答案,我会在可能的时候接受。
function scrape($list_url, $shop_name, $photo_location, $photo_url_root, $product_location, $product_url_root, $was_price_location, $now_price_location, $gender, $country)
{

    header("Content-Type: application/xml; charset=UTF-8");

    $xml = new SimpleXMLElement('<rss/>');
    $xml->addAttribute("version", "2.0");
    $channel = $xml->addChild("channel");

    $html = file_get_contents($list_url);
    $doc = new DOMDocument();
    libxml_use_internal_errors(TRUE);

    if (!empty($html))
    {
        $doc->loadHTML($html);
        libxml_clear_errors(); // remove errors for yucky html
        $xpath = new DOMXPath($doc);
        /* FIND LINK TO PRODUCT PAGE */
        $products = array();
        $entries = array();
/* product_url */
        $row = $xpath->query($product_location);
        if ($row->length > 0)
        {
            foreach ($row as $location)
            {
                $product_url[] = $product_url_root . $location->getAttribute('href');
            }
        }
/* photo_url */
        $imgs = $xpath->query($photo_location);
        if ($imgs->length > 0)
        {
            foreach ($imgs as $img)
            {
                $photo_url[] = $photo_url_root . $img->getAttribute('src');
            }
        }
        $result = array();
        foreach ($product_url as $i => $val)
        {
            $result[] = array(
                'product_url' => $val,
                'shop_name' => $shop_name,
                'photo_url' => $photo_url[$i]
            );
        }

        //print_r($result);

        $item = $channel->addChild("item");
        $item->addChild("product_url", $result['product_url']);
        $item->addChild("shop_name", $result['shop_name']);
        $item->addChild("photo_url", $result['photo_url']);

    }
    echo $xml->asXML();
}