第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

我的 json 對(duì)象只輸出一個(gè)對(duì)象,但我的 xml 文件中有更多對(duì)象

我的 json 對(duì)象只輸出一個(gè)對(duì)象,但我的 xml 文件中有更多對(duì)象

PHP
UYOU 2022-06-11 18:20:33
這是我的 JSON 輸出,我會(huì)喜歡更多對(duì)象[    {        "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}問題是他只輸出一個(gè) Product 對(duì)象,但我的 XML 文件包含其中 4 個(gè)。
查看完整描述

2 回答

?
慕神8447489

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊

您在腳本結(jié)束時(shí)遇到了一些問題,您需要將設(shè)備添加到主循環(huán)內(nèi)的數(shù)組中,但在所有循環(huán)結(jié)束后輸出 JSON(您在第一個(gè)循環(huán)結(jié)束時(shí)返回?cái)?shù)據(jù)) .


// 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



查看完整回答
反對(duì) 回復(fù) 2022-06-11
?
白衣非少年

TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊

你錯(cuò)過了}第一個(gè)結(jié)束foreach。因此return總是在 first 的第一個(gè)循環(huán)內(nèi)執(zhí)行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

}


查看完整回答
反對(duì) 回復(fù) 2022-06-11
  • 2 回答
  • 0 關(guān)注
  • 150 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)