PHP _ php instance for converting XML to array

Source: Internet
Author: User
This article describes how to use PHP to convert an XML object into an array to get an xml object:

The Code is as follows:


$ Resp = $ this-> c-> execute ($ req, $ sessionKey); // obtain the xml Object
$ Items = $ resp-> items;


To read the object value, use $ items-> item, or $ items-> item-> price. This operation is inconvenient and does not conform to the php's habit of operating arrays.

Php provides the array method to convert an object to an array. You only need to add (array) before the object you want to convert the array.

For example, convert $ items-> item (objects with many items) to an array:

The Code is as follows:


Foreach ($ items-> item as $ item ){
$ Goods [] = (array) $ item;
}


$ Goods is a php array.
Before conversion:

The Code is as follows:


SimpleXMLElement Object
(
[Cid] = & gt; 50003793
[Modified] => 17:16:25
[Nick] => qq307819623
[Price] = & gt; 200.00
[Title] => new licensed Nokia N97
)
SimpleXMLElement Object
(
[Cid] = & gt; 50024921
[Modified] => 16:58:06
[Nick] => qq307819623
[Pic_url] => pic.jpg
[Price] = & gt; 888888.00
[Title] => Liu junzhong
)
SimpleXMLElement Object
(
[Cid] = & gt; 1512
[Modified] => 16:56:46
[Nick] => qq307819623
[Pic_url] => item_pic.jpg
[Price] = & gt; 323232.00
[Title] => hello, second-hand
)
SimpleXMLElement Object
(
[Cid] = & gt; 50012166
[Modified] => 15:10:07
[Nick] => qq307819623
[Pic_url] => 0-item_pic.jpg
[Price] = & gt; 32.00
[Title] => ratesky ratsard's crazy View
)


After conversion:

The Code is as follows:


Array
(
[0] => Array
(
[Cid] = & gt; 50003793
[Modified] => 17:16:25
[Nick] => qq307819623
[Price] = & gt; 200.00
[Title] => new licensed Nokia N97
)

[1] => Array
(
[Cid] = & gt; 50024921
[Modified] => 16:58:06
[Nick] => qq307819623
[Pic_url] => pic.jpg
[Price] = & gt; 888888.00
[Title] => Liu junzhong
)

[2] => Array
(
[Cid] = & gt; 1512
[Modified] => 16:56:46
[Nick] => qq307819623
[Pic_url] => item_pic.jpg
[Price] = & gt; 323232.00
[Title] => hello, second-hand
)

[3] => Array
(
[Cid] = & gt; 50012166
[Modified] => 15:10:07
[Nick] => qq307819623
[Pic_url] => 0-item_pic.jpg
[Price] = & gt; 32.00
[Title] => ratesky ratsard's crazy View
)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.