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
)