0

尝试使用下面的代码将产品添加到 $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'));

这是我对 $products 使用 print_r 的 SIMPLEXML 对象:

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
                    (
                    )

            )

    )
4

1 回答 1

1

您将数组作为第二个参数传递给 array_walk_recursive。

来自 PHP 文档:

bool array_walk_recursive ( array &$input , callable $funcname [, mixed $userdata = NULL ] )

第二个参数必须是一个可调用函数名,它接受两个参数,数组的值和键,如果您将它作为第三个参数包含到 array_walk_recursive,则传递第三个可选参数。

换句话说,您将不得不调用类似:

array_walk_recursive($fields, 'funcname', 'addChild');

你可以在这里阅读:http: //php.net/manual/en/function.array-walk-recursive.php

于 2013-04-06T08:01:52.310 回答