尝试使用以下代码将产品添加到$ 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对象,使用print_r for $ products:
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
(
)
)
)
答案 0 :(得分: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