亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我的 json 對象只輸出一個對象,但我的 xml 文件中有更多對象

我的 json 對象只輸出一個對象,但我的 xml 文件中有更多對象

PHP
UYOU 2022-06-11 18:20:33
這是我的 JSON 輸出,我會喜歡更多對象[    {        "EcommerceProductGuid": "1282174A-F6A9-4049-8C03-EFDBF065A22F",        "ProductNumber": "-004394",        "Description": "Sparta Ion DLI D50",        "Type": "Ion DLI M-Gear",        "Kind": "Elektrische fiets",        "Brand": "Sparta",    }]這是我的 PHPfunction XMLtoJSON($xml) {    $xml = file_get_contents($xml);    // gets XML content from file    $xml = str_replace(array("\n", "\r", "\t"), '', $xml);    // removes newlines, returns and tabs    // replace double quotes with single quotes, to ensure the simple XML function can parse the XML    $xml = trim(str_replace('"', "'", $xml));    $simpleXml = simplexml_load_string($xml);    // loop over Products -> Product item in the xml file    $devices = array();    foreach($simpleXml->Products->Product as $product)    {        $device = array();        foreach($product as $key => $value)        {            //$device[(string)$rewriteKeys[$key]] = (string)$value;            $device[(string)$key] = (string)$value;            // unset empty and extra keys            unset($device['epg']);            unset($device[null]);        }    $devices[] = $product;    return stripslashes(json_encode($devices, JSON_PRETTY_PRINT));    // returns a string with JSON object}問題是他只輸出一個 Product 對象,但我的 XML 文件包含其中 4 個。
查看完整描述

2 回答

?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

您在腳本結束時遇到了一些問題,您需要將設備添加到主循環內的數組中,但在所有循環結束后輸出 JSON(您在第一個循環結束時返回數據) .


// loop over Products -> Product item in the xml file

$devices = array();

foreach($simpleXml->Products->Product as $product)

{


    $device = array();

    foreach($product as $key => $value)

    {

        //$device[(string)$rewriteKeys[$key]] = (string)$value;

        $device[(string)$key] = (string)$value;

    }

    // unset empty and extra keys after transferring all values

    unset($device['epg']);

    unset($device[null]);


    // Add device into array inside loop

    $devices[] = $product;

}


// Return data after processing all product in loop

return stripslashes(json_encode($devices, JSON_PRETTY_PRINT));    // returns a string with JSON object



查看完整回答
反對 回復 2022-06-11
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

你錯過了}第一個結束foreach。因此return總是在 first 的第一個循環內執行foreach:


function XMLtoJSON($xml) {

    $xml = file_get_contents($xml);    // gets XML content from file

    $xml = str_replace(array("\n", "\r", "\t"), '', $xml);    // removes newlines, returns and tabs


    // replace double quotes with single quotes, to ensure the simple XML function can parse the XML

    $xml = trim(str_replace('"', "'", $xml));

    $simpleXml = simplexml_load_string($xml);



    // loop over Products -> Product item in the xml file

    $devices = array();

    foreach($simpleXml->Products->Product as $product)

    {


        $device = array();

        foreach($product as $key => $value)

        {

            //$device[(string)$rewriteKeys[$key]] = (string)$value;

            $device[(string)$key] = (string)$value;


            // unset empty and extra keys

            unset($device['epg']);

            unset($device[null]);

        }

    $devices[] = $product;

    } // THIS WAS MISSING


    return stripslashes(json_encode($devices, JSON_PRETTY_PRINT));    // returns a string with JSON object

}


查看完整回答
反對 回復 2022-06-11
  • 2 回答
  • 0 關注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號