得到一個xml型的物件:
$resp = $this->c->execute($req, $sessionKey);//获得xml对象 $items=$resp->items;那麼讀取物件的值,就用$items->item,或是$items->item->price,如此操作很不方便,不符合php操作陣列的習慣。
php提供了array方法將物件轉換成數組,只要把你要轉換數組的物件前面加上(array)就行了。
例如將$items->item(有很多item的物件)轉換成陣列:
foreach ($items->item as $item){ $goods[]=(array)$item; }
$goods就是一個php陣列了。
轉換前:
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] => 放大率拉德斯基分拉沙德疯了似的看法拉斯法 )
轉換後:
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轉數組過程詳解相關文章請關注PHP中文網!