[英]XML/Array to Key value pair in PHP
我有以下輸出:
<table count="" time="0.010006904602051">
<item>
<id>607</id>
<name>MSPOT6071</name>
<description>Hip Hop / Raps</description>
<type>3</type>
<radio_folder_id/>
<albumart>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_175.jpg
</albumart>
<albumart_300>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_350.jpg
</albumart_300>
<albumart_500>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_500.jpg
</albumart_500>
</item>
<item>
<id>48542614</id>
<name>US Pop - TESTB</name>
<description>Blues</description>
<type>3</type>
<radio_folder_id/>
</item>
</table>
我想以鍵值對的形式讀取此輸出,然后要存儲每個值。 例如,我想要“名稱”的值
誰能幫我提供代碼。.我不知道該怎么做的線索。
您可以使用simplexml擴展功能來實現您的目標。 使用此擴展,您可以從文件 , 字符串或html節點加載xml。 單擊鏈接以獲取有關每個加載功能的更多信息。 我將在這里說明如何使用simplexml_load_file進行加載。 這段代碼:
<?php
echo "<pre>";
$xml = simplexml_load_file("name.xml");
print_r($xml);
echo "</pre>";
?>
將返回此:
SimpleXMLElement Object
(
[@attributes] => Array
(
[count] =>
[time] => 0.011766910552979
)
[item] => Array
(
[0] => SimpleXMLElement Object
(
[id] => 607
[name] => MSPOT6071
[description] => Hip Hop / Raps
[type] => 3
[radio_folder_id] => SimpleXMLElement Object
(
)
[albumart] => http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_175.jpg
[albumart_300] => http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_350.jpg
[albumart_500] => http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_500.jpg
)
[1] => SimpleXMLElement Object
(
[id] => 48542614
[name] => US Pop - TESTB
[description] => Blues
[type] => 3
[radio_folder_id] => SimpleXMLElement Object
(
)
)
)
)
您可以通過以下方式訪問所需的信息:
<?php
echo "<pre>";
$xml = simplexml_load_file("name.xml");
print_r($xml);
echo "</pre>";
echo "<pre>";
foreach($xml->children() as $item){
$arr = get_object_vars($item);
foreach($arr as $key=>$value){
echo "$key => $value" . PHP_EOL;
}
}
echo "</pre>";
?>
請注意,它將首先作為對象進行訪問。 然后,您可以獲取所有對象變量以動態瀏覽對象屬性。
該
<?php
$xmlstr = '<?xml version="1.0" standalone="yes"?>
<table count="" time="0.010006904602051">
<item>
<id>607</id>
<name>MSPOT6071</name>
<description>Hip Hop / Raps</description>
<type>3</type>
<radio_folder_id/>
<albumart>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_175.jpg
</albumart>
<albumart_300>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_350.jpg
</albumart_300>
<albumart_500>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_500.jpg
</albumart_500>
</item>
<item>
<id>48542614</id>
<name>US Pop - TESTB</name>
<description>Blues</description>
<type>3</type>
<radio_folder_id/>
</item>
</table>';
$xml = new SimpleXMLElement($xmlstr);
foreach($xml->item as $item)
{
echo $item->name."<br>";
}
echo $xml->item[0]->name;
echo '<pre>';
print_r($xml);
echo '</pre>';
?>
將XML字符串分配給變量$ xmlstr,這樣就不會遇到不完整的XML文檔錯誤,請確保在XML文檔的頂部包含以下內容。
<?xml version="1.0" standalone="yes"?>
然后通過將XML字符串$ xmlstr傳遞給SimpleXML來使用內置的SimpleXML類:
$xml = new SimpleXMLElement($xmlstr);
現在,您可以使用SimpleXML類的屬性和方法將XML文件作為PHP對象訪問。 在此示例中,我們遍歷XML文檔中的“項目”並打印出“名稱”元素:
foreach($xml->item as $item)
{
echo $item->name."<br>";
}
我還包含用於訪問第一個item元素的代碼:
echo $xml->item[0]->name;
以及一些調試代碼,在SimpleXML對象中查看XML文檔:
echo '<pre>';
print_r($xml);
echo '</pre>';
您可以通過鍵名來訪問鍵或對象屬性(在這種情況下)。 因此,在您的foreach循環中,您可以執行以下操作:
if($item->name)
{
echo $item->name;
}
要么
if($item->description)
{
echo $item->description;
}
願原力與你同在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.