- 相關推薦
PHP將XML轉(zhuǎn)為數(shù)組的方法詳解
本文詳細介紹了,在php編程中,將XML數(shù)據(jù)轉(zhuǎn)換為數(shù)組的詳細過程,剖析了xml轉(zhuǎn)為php數(shù)組的具體方法,感興趣的朋友可以參考學習下。就跟隨百分網(wǎng)小編一起去了解下吧,想了解更多相關信息請持續(xù)關注我們應屆畢業(yè)生考試網(wǎng)!
PHP XML轉(zhuǎn)數(shù)組
首先,得到一個xml型的對象:
復制代碼 代碼示例:
$resp = $this->c->execute($req, $sessionKey);//獲得xml對象
$items=$resp->items;
針對這種方法:
讀取對象的值,就用$items->item,或$items->item->price,如此操作很不方便,不符合php操作數(shù)組的習慣。
php提供了array方法將對象轉(zhuǎn)換成數(shù)組,只需把轉(zhuǎn)換數(shù)組的對象前面加上(array)即可。
例如,將$items->item(有很多item的對象)轉(zhuǎn)換成數(shù)組:
復制代碼 代碼示例:
<?php
foreach ($items->item as $item){
$goods[]=(array)$item;
}
$goods就是一個php數(shù)組了。
轉(zhuǎn)換前:
SimpleXMLElement Object
(
[cid] => 50003793
[modified] => 2013-04-18 17:16:25
[nick] => qq307819623
[price] => 200.00
[title] => Nokia N97全新行貨
)
SimpleXMLElement Object
(
[cid] => 50024921
[modified] => 2013-04-18 16:58:06
[nick] => qq307819623
[pic_url] =>pic.jpg
[price] => 888888.00
[title] => 劉俊仲
)
SimpleXMLElement Object
(
[cid] => 1512
[modified] => 2013-04-18 16:56:46
[nick] => qq307819623
[pic_url] => item_pic.jpg
[price] => 323232.00
[title] => 二手你好
)
SimpleXMLElement Object
(
[cid] => 50012166
[modified] => 2013-04-18 15:10:07
[nick] => qq307819623
[pic_url] =>0-item_pic.jpg
[price] => 32.00
[title] => 放大率拉德斯基分拉沙德瘋了似的看法拉斯法
)
轉(zhuǎn)換后:
Array
(
[0] => Array
(
[cid] => 50003793
[modified] => 2013-04-18 17:16:25
[nick] => qq307819623
[price] => 200.00
[title] => Nokia N97全新行貨
)
[1] => Array
(
[cid] => 50024921
[modified] => 2013-04-18 16:58:06
[nick] => qq307819623
[pic_url] => pic.jpg
[price] => 888888.00
[title] => 劉俊仲
)
[2] => Array
(
[cid] => 1512
[modified] => 2013-04-18 16:56:46
[nick] => qq307819623
[pic_url] =>item_pic.jpg
[price] => 323232.00
[title] => 二手你好
)
[3] => Array
(
[cid] => 50012166
[modified] => 2013-04-18 15:10:07
[nick] => qq307819623
[pic_url] => 0-item_pic.jpg
[price] => 32.00
[title] => 放大率拉德斯基分拉沙德瘋了似的看法拉斯法
)
【PHP將XML轉(zhuǎn)為數(shù)組的方法詳解】相關文章:
php數(shù)組長度的方法10-05
PHP自帶的幾個實用的數(shù)組函數(shù)詳解10-07
php遞歸遍歷多維數(shù)組的方法10-06
PHP超全局數(shù)組的方法10-29
PHP數(shù)據(jù)類型之數(shù)組變量詳解10-04
PHP數(shù)組函數(shù)知識10-24
PHP數(shù)組長度的技巧09-18