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

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

PHP DOM XML 到數(shù)組的轉(zhuǎn)換

PHP DOM XML 到數(shù)組的轉(zhuǎn)換

PHP
BIG陽 2023-07-21 18:15:36
我有一個 xml 文檔如下:<?xml version="1.0" encoding="UTF-8" ?><books>    <book>        <name>Title One</name>        <year>2014</year>        <authors>            <author>                <name>Author One</name>            </author>        </authors>    </book>    <book serie="yes">        <name>Title Two</name>        <year>2015</year>        <authors>            <author>                <name>Author two</name>            </author>            <author>                <name>Author three</name>            </author>        </authors>    </book>    <book serie="no">        <name>Title Three</name>        <year>2015</year>        <authors>            <author>                <name>Author four</name>            </author>        </authors>    </book></books>我想將它轉(zhuǎn)換成下面的數(shù)組。array(    array('Tittle one', 2014, 'Author One'),    array('Tittle two', 2015, 'Author two, Author three'),    array('Tittle three', 2015, 'Author four'),);我下面的代碼無法生成我想要的數(shù)組結(jié)構(gòu):function arrayRepresentation(){    $xmldoc = new DOMDocument();    $xmldoc->load("data/data.xml");    $parentArray =  array();    foreach ($xmldoc->getElementsByTagName('book') as $item) {        $parentArray[] = array_generate($item);    }    var_dump($parentArray);}function array_generate($item){    $movieArray = array();    $childMovieArray = array();    for ($i = 0; $i < $item->childNodes->length; ++$i) {        $child = $item->childNodes->item($i);        if ($child->nodeType == XML_ELEMENT_NODE) {            if(hasChild($child)){                $childMovieArray = array_generate($child);            }        }        $movieArray[] = trim($child->nodeValue);    }        if(!empty($childMovieArray)){        $movieArray = array_merge($movieArray,$childMovieArray);    }        return $movieArray;}基本上我循環(huán)遍歷節(jié)點來獲取 xml 元素值。然后我檢查我的節(jié)點是否有更多的子節(jié)點,如果有,我再次循環(huán)它以獲取值。我無法推斷出一種方法:(i)不會給我一些空數(shù)組元素(ii)檢查任何子節(jié)點并將所有 xml 同級元素放入單個字符串中
查看完整描述

1 回答

?
慕碼人2483693

TA貢獻1860條經(jīng)驗 獲得超9個贊

PHP DOM 支持 Xpath 表達式來獲取特定節(jié)點和值。這大大減少了您需要的循環(huán)和條件的數(shù)量。


這是一個演示:


// bootstrap the XML document

$document = new DOMDocument();

$document->loadXML($xml);

$xpath = new DOMXpath($document);


$data = [];

// iterate the node element nodes

foreach ($xpath->evaluate('/books/book') as $book) {

    $authors = array_map(

       fn ($node) => $node->textContent,

       // fetch author name nodes as an array

       iterator_to_array($xpath->evaluate('authors/author/name', $book))

    );

    $data[] = [

        // cast first name element child to string

        $xpath->evaluate('string(name)', $book),

        // cast first year element child to string

        $xpath->evaluate('string(year)', $book),

        implode(', ', $authors)

    ];

}


var_dump($data);


查看完整回答
反對 回復(fù) 2023-07-21
  • 1 回答
  • 0 關(guān)注
  • 137 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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