PHP將XML轉數組過程詳解

來源:互聯網
上載者:User
得到一個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] => 放大率拉德斯基分拉沙德瘋了似的看法拉斯法        )

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.