PHP如何將XML轉成數組
來源:易賢網 閱讀:1206 次 日期:2016-08-27 13:30:45
溫馨提示:易賢網小編為您整理了“PHP如何將XML轉成數組”,方便廣大網友查閱!

如果你使用 curl 獲取的 xml data

xml=simplexmlloadstring(data);

data[′tk′]=jsondecode(jsonencode(xml),TRUE);

如果是直接獲取 URL 數據的話

xml=simplexmlloadfile(data);

data[′tk′]=jsondecode(jsonencode(xml),TRUE);

先把 simplexml 對象轉換成 json,再將 json 轉換成數組。

代碼:

<?php

$string = <<<XML

<?xml version='1.0'?> 

<document>

 <title>Forty What?</title>

 <from>Joe</from>

 <to>Jane</to>

 <body>

 I know that's the answer -- but what's the question?

 </body>

</document>

XML;

$xml=simplexml_load_string($string);

$data = json_decode(json_encode($xml),TRUE);

var_dump( $xml );

var_dump( $data );

二.

object(SimpleXMLElement)[1]

 public 'title' => string 'Forty What?' (length=11)

 public 'from' => string 'Joe' (length=3)

 public 'to' => string 'Jane' (length=4)

 public 'body' => string '

 I know that's the answer -- but what's the question?

 ' (length=57)

array

 'title' => string 'Forty What?' (length=11)

 'from' => string 'Joe' (length=3)

 'to' => string 'Jane' (length=4)

 'body' => string '

 I know that's the answer -- but what's the question?

 ' (length=57)

以上就是本文的全部內容,希望對大家的學習有所幫助。

更多信息請查看網絡編程

扫描二维码关注易贤网公众号

易賢網手機網站地址:PHP如何將XML轉成數組

2026國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼

【备考推荐】2025年事业单位联考系统精讲课,免费试看,配套资料包邮到家

關于我們 | 聯(lián)系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網