Home  >  Article  >  Backend Development  >  Detailed explanation of the process of converting XML to array in PHP_PHP Tutorial

Detailed explanation of the process of converting XML to array in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:13765browse

Get an xml object:

Copy code The code is as follows:

$resp = $this-> c->execute($req, $sessionKey);//Get the xml object
$items=$resp->items;

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

php provides the array method to convert objects into arrays. Just add (array) in front of the object you want to convert to an array.

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

Copy the code The code is as follows:

foreach ($items->item as $item){
            $goods[]=(array)$item;
}

$goods is a php array .
Before conversion:
Copy code The code is as follows:

SimpleXMLElement Object
(
[cid] => 50003793
[modified] => 2013-04-18 17:16:25
[nick] => qq307819623
[price] => 200.00
[title] = > Nokia N97 brand 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 [pic_url] = > item_pic.jpg
[price] => 323232.00
[title] => Second-hand 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 Radeski points Rashad like crazy to Falassfa
)


After conversion:


Copy code

The code is as follows:

Array
(
    [0] => Array
        (
            [cid] => 50003793
            [modified] => 2013-04-18 17:16:25
            [nick] => qq307819623
            [price] => 200.00
            [title] => Nokia N97全新行货
        )

    [1] => Array
        (
            [cid] => 50024921
            [modified] => 2013-04-18 16:58:06
            [nick] => qq307819623
            [pic_url] => pic.jpg
            [price] => 888888.00
            [title] => 刘俊仲
        )

    [2] => Array
        (
            [cid] => 1512
            [modified] => 2013-04-18 16:56:46
            [nick] => qq307819623
            [pic_url] =>item_pic.jpg
            [price] => 323232.00
            [title] => 二手你好
        )

    [3] => Array
        (
            [cid] => 50012166
            [modified] => 2013-04-18 15:10:07
            [nick] => qq307819623
            [pic_url] => 0-item_pic.jpg
            [price] => 32.00
            [title] => 放大率拉德斯基分拉沙德疯了似的看法拉斯法
        )

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/825176.htmlTechArticle得到一个xml型的对象: 复制代码 代码如下: $resp = $this-c-execute($req, $sessionKey);//获得xml对象 $items=$resp-items; 那么读取对象的值,就用$items-i...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn