PHP _php An example of XML-to-array process

Source: Internet
Author: User
Get an XML-type object:
Copy CodeThe code is as follows:
$resp = $this->c->execute ($req, $sessionKey);//Get an XML object
$items = $resp->items;

Then read the value of the object, with $items->item, or $items->item->price, so the operation is inconvenient, does not conform to the habit of PHP operation array.

PHP provides an array method to convert an object to an array, as long as you add (array) to the object that you want to convert.

For example, convert $items->item (objects with many item) to an array:
Copy CodeThe code is as follows:
foreach ($items->item as $item) {
$goods []= (array) $item;
}

$goods is an array of PHP.
Before conversion:
Copy CodeThe code is as follows:
SimpleXMLElement Object
(
[CID] = 50003793
[Modified] = 2013-04-18 17:16:25
[Nick] = qq307819623
[Price] = 200.00
[title] = Nokia N97 new Licensed
)
SimpleXMLElement Object
(
[CID] = 50024921
[Modified] = 2013-04-18 16:58:06
[Nick] = qq307819623
[Pic_url] =>pic.jpg
[Price] = 888888.00
[title] = Liu Junzhong
)
SimpleXMLElement Object
(
[CID] = 1512
[Modified] = 2013-04-18 16:56:46
[Nick] = qq307819623
[Pic_url] = item_pic.jpg
[Price] = 323232.00
[title] = used Hello
)
SimpleXMLElement Object
(
[CID] = 50012166
[Modified] = 2013-04-18 15:10:07
[Nick] = qq307819623
[Pic_url] =>0-item_pic.jpg
[Price] = 32.00
[title] = magnification Radetzky a crazy view of the Dallas law
)

After conversion:
Copy CodeThe code is as follows:
Array
(
[0] = = Array
(
[CID] = 50003793
[Modified] = 2013-04-18 17:16:25
[Nick] = qq307819623
[Price] = 200.00
[title] = Nokia N97 new Licensed
)

[1] = = Array
(
[CID] = 50024921
[Modified] = 2013-04-18 16:58:06
[Nick] = qq307819623
[Pic_url] = pic.jpg
[Price] = 888888.00
[title] = Liu Junzhong
)

[2] = = Array
(
[CID] = 1512
[Modified] = 2013-04-18 16:56:46
[Nick] = qq307819623
[Pic_url] =>item_pic.jpg
[Price] = 323232.00
[title] = used Hello
)

[3] = = Array
(
[CID] = 50012166
[Modified] = 2013-04-18 15:10:07
[Nick] = qq307819623
[Pic_url] = 0-item_pic.jpg
[Price] = 32.00
[title] = magnification Radetzky a crazy view of the Dallas law
)

  • 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.