Php 如何添加到SimpleXML对象

Php 如何添加到SimpleXML对象,php,simplexml,Php,Simplexml,尝试使用下面的代码将产品添加到$products SimpleXML对象时 我收到一个错误警告:文件中的偏移量类型非法,无法确定是什么 这是错误的 $fields = array( $o->order_id => 'order_id', $o->order_address_id => 'order_address_id', $o->price_ex_tax => 'pr

尝试使用下面的代码将产品添加到$products SimpleXML对象时 我收到一个错误警告:文件中的偏移量类型非法,无法确定是什么 这是错误的

          $fields = array(
            $o->order_id => 'order_id',
            $o->order_address_id => 'order_address_id',
            $o->price_ex_tax => 'price_ex_tax' ,
            'DISCS' => 'sku',
             '1' => 'quantity'
        );                            

        $products->addChild('product');
        array_walk_recursive($fields, array ($products, 'addChild'));
下面是我的SIMPLEXML对象,它使用$products的print\r:

SimpleXMLElement Object
(
    [product] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [id] => 23
                [order_id] => 110
                [product_id] => 63477
                [order_address_id] => 11
                [name] => Dead Sea Treasures Foot Cream
                [sku] => 703
                [type] => physical
                [base_price] => 20.0000
                [price_ex_tax] => 20.0000
                [price_inc_tax] => 20.0000
                [price_tax] => 0.0000
                [base_total] => 40.0000
                [total_ex_tax] => 40.0000
                [total_inc_tax] => 40.0000
                [total_tax] => 0.0000
                [weight] => 0.5
                [quantity] => 2
                [base_cost_price] => 0.0000
                [cost_price_inc_tax] => 0.0000
                [cost_price_ex_tax] => 0.0000
                [cost_price_tax] => 0.0000
                [is_refunded] => false
                [refund_amount] => 0.0000
                [return_id] => 0
                [wrapping_name] => SimpleXMLElement Object
                    (
                    )

                [base_wrapping_cost] => 0.0000
                [wrapping_cost_ex_tax] => 0.0000
                [wrapping_cost_inc_tax] => 0.0000
                [wrapping_cost_tax] => 0.0000
                [wrapping_message] => SimpleXMLElement Object
                    (
                    )

                [quantity_shipped] => 0
                [event_name] => NULL
                [event_date] => SimpleXMLElement Object
                    (
                    )

                [fixed_shipping_cost] => 0.0000
                [ebay_item_id] => SimpleXMLElement Object
                    (
                    )

                [ebay_transaction_id] => SimpleXMLElement Object
                    (
                    )

                [option_set_id] => NULL
                [parent_order_product_id] => NULL
                [is_bundled_product] => false
                [bin_picking_number] => SimpleXMLElement Object
                    (
                    )

                [applied_discounts] => SimpleXMLElement Object
                    (
                        [discount] => SimpleXMLElement Object
                            (
                                [id] => 1
                                [amount] => 4
                            )

                    )

                [product_options] => SimpleXMLElement Object
                    (
                    )

                [configurable_fields] => SimpleXMLElement Object
                    (
                    )

            )

        [1] => SimpleXMLElement Object
            (
                [id] => 24
                [order_id] => 110
                [product_id] => 63398
                [order_address_id] => 11
                [name] => Naot Matai
                [sku] => 11410-37-577
                [type] => physical
                [base_price] => 166.0000
                [price_ex_tax] => 166.0000
                [price_inc_tax] => 166.0000
                [price_tax] => 0.0000
                [base_total] => 166.0000
                [total_ex_tax] => 166.0000
                [total_inc_tax] => 166.0000
                [total_tax] => 0.0000
                [weight] => 2
                [quantity] => 1
                [base_cost_price] => 0.0000
                [cost_price_inc_tax] => 0.0000
                [cost_price_ex_tax] => 0.0000
                [cost_price_tax] => 0.0000
                [is_refunded] => false
                [refund_amount] => 0.0000
                [return_id] => 0
                [wrapping_name] => SimpleXMLElement Object
                    (
                    )

                [base_wrapping_cost] => 0.0000
                [wrapping_cost_ex_tax] => 0.0000
                [wrapping_cost_inc_tax] => 0.0000
                [wrapping_cost_tax] => 0.0000
                [wrapping_message] => SimpleXMLElement Object
                    (
                    )

                [quantity_shipped] => 0
                [event_name] => NULL
                [event_date] => SimpleXMLElement Object
                    (
                    )

                [fixed_shipping_cost] => 0.0000
                [ebay_item_id] => SimpleXMLElement Object
                    (
                    )

                [ebay_transaction_id] => SimpleXMLElement Object
                    (
                    )

                [option_set_id] => 32446
                [parent_order_product_id] => NULL
                [is_bundled_product] => false
                [bin_picking_number] => SimpleXMLElement Object
                    (
                    )

                [applied_discounts] => SimpleXMLElement Object
                    (
                    )

                [product_options] => SimpleXMLElement Object
                    (
                        [option] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [id] => 57
                                        [option_id] => 49564
                                        [product_option_id] => 36782
                                        [display_name] => Womens Shoe Size
                                        [display_value] => 37 (US6)
                                        [value] => 277224
                                        [type] => SimpleXMLElement Object
                                            (
                                            )

                                        [name] => SimpleXMLElement Object
                                            (
                                            )

                                        [display_style] => SimpleXMLElement Object
                                            (
                                            )

                                    )

                                [1] => SimpleXMLElement Object
                                    (
                                        [id] => 58
                                        [option_id] => 49565
                                        [product_option_id] => 36783
                                        [display_name] => Shoe Color
                                        [display_value] => Copper/Wine Suede
                                        [value] => 277232
                                        [type] => SimpleXMLElement Object
                                            (
                                            )

                                        [name] => SimpleXMLElement Object
                                            (
                                            )

                                        [display_style] => SimpleXMLElement Object
                                            (
                                            )

                                    )

                            )

                    )

                [configurable_fields] => SimpleXMLElement Object
                    (
                    )

            )

    )

将数组作为第二个参数传递给array\u walk\u recursive

从PHP文档中:

bool array_walk_recursive ( array &$input , callable $funcname [, mixed $userdata = NULL ] )
第二个参数必须是一个可调用的函数名,该函数名接受两个参数,即数组的value和key以及第三个可选参数,如果将其作为第三个参数包含到array\u walk\u recursive中,则会传递第三个可选参数

换言之,你必须称之为:

array_walk_recursive($fields, 'funcname', 'addChild');
您可以在此处阅读: