PHP如何將XML字串轉陣列?
首先使用函數“simplexml_load_string()”,將XML字串轉為物件;
$obj = simplexml_load_string($str,"SimpleXMLElement", LIBXML_NOCDATA);
然後使用“json_encode()”函數將物件轉換為JSON字串;
$json_str = json_encode($obj);
最後使用「json_decode()」轉為陣列即。
$xml_arr = json_decode($json_str, true);
完整程式碼
$str = ''; $obj = simplexml_load_string($str,"SimpleXMLElement", LIBXML_NOCDATA); $test = json_decode(json_encode($obj),true); $arr = [ 'FromUserName' => $test['FromUserName'], 'ToUserName' => $test['ToUserName'], 'CreateTime' => $test['CreateTime'], 'CardId' => $test['CardId'], 'UserCardCode' => $test['UserCardCode'], 'ConsumeSource' => $test['ConsumeSource'], 'StaffOpenId' => $test['StaffOpenId'] ]; $arr = array_map('trim',$arr); var_dump($arr); gh_fc0a06a20993 oZI8Fj040-be6rlDohc6gkoPOQTQ 1472549042 event user_consume_card pZI8Fj8y-E8hpvho2d1ZvpGwQBvA 452998530302 FROM_API oZ********nJ3bPJu_Rtjkw4c xxxxx
推薦教學:《PHP》
以上是PHP如何將XML字串轉數組?的詳細內容。更多資訊請關注PHP中文網其他相關文章!