PHP detailed the process of converting XML to array. Get an xml object: Copy the code as follows: $ resp $ this-c-execute ($ req, $ sessionKey); get the xml object $ items $ resp-items; to read the object value, use $ items-I 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
)
The authorization code is as follows: $ resp = $ this-c-execute ($ req, $ sessionKey); // Get the xml object $ items = $ resp-items; then read the object value, use $ items-I...