将SimpleXMLElement对象转换为数组

时间:2017-05-17 05:53:57

标签: php arrays

我必须将SimpleXMLElement对象转换为数组。

数组如下:

Array
(
[0] => SimpleXMLElement Object
    (
        [Cell] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [Data] => admin
                    )

                [1] => SimpleXMLElement Object
                    (
                        [Data] => base
                    )

                [2] => SimpleXMLElement Object
                    (
                        [Data] => Books
                    )

                [3] => SimpleXMLElement Object
                    (
                        [Data] => simple
                    )

                [4] => SimpleXMLElement Object
                    (
                        [Data] => 2,4,276,280
                    )

                [5] => SimpleXMLElement Object
                    (
                        [Data] => ACKTINDD20
                    )

                [6] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [7] => SimpleXMLElement Object
                    (
                        [Data] => Tinkle Double Digest Assorted (Pack of 20) + FREE Customized Mug
                    )

                [8] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [9] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [10] => SimpleXMLElement Object
                    (
                        [Data] => /p/r/product-image_1__1.jpg
                    )

                [11] => SimpleXMLElement Object
                    (
                        [Data] => /p/r/product-image_1__1.jpg
                    )

                [12] => SimpleXMLElement Object
                    (
                        [Data] => /p/r/product-image_1__1.jpg
                    )

                [13] => SimpleXMLElement Object
                    (
                        [Data] => tinkle-double-digest-assorted-pack-of-20-free-customized-mug
                    )

                [14] => SimpleXMLElement Object
                    (
                        [Data] => tinkle-double-digest-assorted-pack-of-20-free-customized-mug
                    )

                [15] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [16] => SimpleXMLElement Object
                    (
                        [Data] => 1 column no container CMS DEFAULT
                    )

                [17] => SimpleXMLElement Object
                    (
                        [Data] => Product Info Column
                    )

                [18] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [19] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [20] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [21] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                                [0] =>  
                            )

                    )

                [22] => SimpleXMLElement Object
                    (
                        [Data] => Use config
                    )

                [23] => SimpleXMLElement Object
                    (
                        [Data] => Use config
                    )

                [24] => SimpleXMLElement Object
                    (
                        [Data] => No
                    )

                [25] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [26] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [27] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [28] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [29] => SimpleXMLElement Object
                    (
                        [Data] => Comics
                    )

                [30] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [31] => SimpleXMLElement Object
                    (
                        [Data] => Rajani Thindiath
                    )

                [32] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [33] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [34] => SimpleXMLElement Object
                    (
                        [Data] => Teens & above
                    )

                [35] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [36] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [37] => SimpleXMLElement Object
                    (
                        [Data] => <p>Best of Tinkle Double Digest Assorted (Pack of 20) + FREE Customised Mug<p>
<p>Make this August even more special! Now get a FREE Customized Coffee Mug with a Tinkle Double Digest Assorted Pack of 20! What could be better than coffee, comics and your friend's company! <p>
<p>Tinkle Double Digest is a 180-page comic book which is published on a monthly basis. It is a collection of the best stories from the past issues of Tinkle Magazine, Tinkle Digest and Tinkle Holiday Specials. You can find stories of all the classic Tinkle toons, the new Tinkle Magazine toons and the original Tinkle Digest toons in every issue of Tinkle Double Digest. <p>

<p>Terms and Conditions: <p>
<p>1. Apply coupon code during checkout to get the discount calculated. 
<p>2. Offer only valid on White Coffee Mugs and not on any other mug variants. 
<p>3. Coupon code valid till October 31, 2016.
<p>4. Coupon code can only be used once per user. 

<p>Product link: http://www.zoomin.com/in/whitecoffeemug<p>

<p>Here are the steps to place an order:
<p>1. Select the theme for the Mug that you wish to order for. You can 
either choose from the pre-designed themes such as Dad, Mom, Rakhi, Love etc 
or else you can choose to create a custom theme yourself. Once you have made 
your theme selection select and click on the "Create" tab. <p>
<p>2. You will then be directed to the product builder page to complete your creation. Once done preview your Project and add your creation to the Cart. <p>
<p>3. Enter the coupon code under the discount section and hit apply. The Discount will automatically be computed under your Cart Summary. Click on Continue Checkout. <p>
<p>4. Enter your shipping address and as it is going to be a Free order click on Continue Payment and your order will get placed. <p>

                    )

                [38] => SimpleXMLElement Object
                    (
                        [Data] => <span></span>
                    )

                [39] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [40] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [41] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [42] => SimpleXMLElement Object
                    (
                        [Data] => 2400.0000
                    )

                [43] => SimpleXMLElement Object
                    (
                        [Data] => 1999.0000
                    )

                [44] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [45] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [46] => SimpleXMLElement Object
                    (
                        [Data] => Disabled
                    )

                [47] => SimpleXMLElement Object
                    (
                        [Data] => No
                    )

                [48] => SimpleXMLElement Object
                    (
                        [Data] => Catalog, Search
                    )

                [49] => SimpleXMLElement Object
                    (
                        [Data] => None
                    )

                [50] => SimpleXMLElement Object
                    (
                        [Data] => English
                    )

                [51] => SimpleXMLElement Object
                    (
                        [Data] => Paperback
                    )

                [52] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [53] => SimpleXMLElement Object
                    (
                        [Data] => Yes
                    )

                [54] => SimpleXMLElement Object
                    (
                        [Data] => 2015
                    )

                [55] => SimpleXMLElement Object
                    (
                        [Data] => Amar Chitra Katha
                    )

                [56] => SimpleXMLElement Object
                    (
                        [Data] => No
                    )

                [57] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [58] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [59] => SimpleXMLElement Object
                    (
                        [Data] => No
                    )

                [60] => SimpleXMLElement Object
                    (
                        [Data] => 2016-07-26 00:00:00
                    )

                [61] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [62] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [63] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [64] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [65] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [66] => SimpleXMLElement Object
                    (
                        [Data] => 0.0000
                    )

                [67] => SimpleXMLElement Object
                    (
                        [Data] => 0.0000
                    )

                [68] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [69] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [70] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [71] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [72] => SimpleXMLElement Object
                    (
                        [Data] => 1.0000
                    )

                [73] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [74] => SimpleXMLElement Object
                    (
                        [Data] => 0.0000
                    )

                [75] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [76] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [77] => SimpleXMLElement Object
                    (
                        [Data] => 2016-09-09 06:15:06
                    )

                [78] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [79] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [80] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [81] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [82] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [83] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [84] => SimpleXMLElement Object
                    (
                        [Data] => 0.0000
                    )

                [85] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [86] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [87] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [88] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [89] => SimpleXMLElement Object
                    (
                        [Data] => 1
                    )

                [90] => SimpleXMLElement Object
                    (
                        [Data] => Tinkle Double Digest Assorted (Pack of 20) + FREE Customized Mug
                    )

                [91] => SimpleXMLElement Object
                    (
                        [Data] => 0
                    )

                [92] => SimpleXMLElement Object
                    (
                        [Data] => simple
                    )

                [93] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

                [94] => SimpleXMLElement Object
                    (
                        [Data] => SimpleXMLElement Object
                            (
                            )

                    )

            )

    )

)

将此转换为数组我正在使用以下功能

function xmlToArray ( $xmlObj, $output = array () )
   {      
   foreach ( (array) $xmlObj as $index => $node )
   {
    $output[$index] = (is_object($node)) ? xmlToArray($node): $node;
   }
  return $output;
}

但是由于没有得到正确的数组而且它将[Cell]元素作为SimpleXMLElement对象。

请为此提出一些解决方案。

2 个答案:

答案 0 :(得分:1)

我正在使用以下代码段从 SimpleXMLElement 对象数组中提取值

array_flatten( json_decode ( json_encode( $simple_xml_object_var ), true ))

解决方案生成一个平面数组

Array
(
    [0] => Analyst Color
    [1] => Penny Stocks
    [2] => Short Sellers
    [3] => Analyst Ratings
)
    

这里是$simple_xml_object_var的结构

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [is_array] => true
        )

    [item] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [name] => Analyst Color
                )

            [1] => SimpleXMLElement Object
                (
                    [name] => Penny Stocks
                )

            [2] => SimpleXMLElement Object
                (
                    [name] => Short Sellers
                )

            [3] => SimpleXMLElement Object
                (
                    [name] => Analyst Ratings
                )

        )

)

答案 1 :(得分:0)

$simpleXmlElementSimpleXMLElement 对象

print_r(json_decode(json_encode(($simpleXmlElement), true), JSON_PRETTY_PRINT));