嵌套数组和json_encode

时间:2017-06-24 03:30:30

标签: php arrays json wordpress

我仍然是PHP和WordPress的新手,我正在努力正确地对一个拥有多个woocommerce订单项的数组进行json_encode。 "订购商品"数组的一部分是我无法弄清楚的部分,因为结构需要根据顺序中的SKU数量动态创建(即1,2,3等)。

我需要创建的数组的动态部分示例如下。

 $product_list_example_array =
     array (
      'items' => array (
                 0 => 
                 array (
                   'quantity' => 1,
                   'sku' => 'sample string 2',
                   'description' => 'sample string 3',
                   'price' => 4,
                 ),
                 1 => 
                 array (
                   'quantity' => 1,
                   'sku' => 'sample string 2',
                   'description' => 'sample string 3',
                   'price' => 4,
                 ),
           ),
     );

为此,我创建了变量$ product_list,它根据产品数量为我的数组提供了完美的格式。当我回到$ product_list时,它看起来与上面的示例完全一样,只是在一行上。

所以我觉得它会很好...但是,当我将$ product_list放入更大的数组中,然后使用json_encode将该数组转换为json时,请注意,我的$ product_list变量只是没有&#39 ; t转换。最终json中出现的$ product_list仍然是数组格式!

foreach( $order_item as $product ) {
$product_name = $product['name'];
$product_qty = $product['qty'];
$product_id = $product['product_id'];
$product_price = $product['line_total'];
$product_sku = get_post_meta( $product_id, '_sku', true);

    $prodct_list_name[] = '['. $items_number++ .']=> array (\'quantity\' => '. $product_qty .',\'sku\' => '. $product_sku .',\'description\' => '. $product_name .'\'price\' => '. $product_price .')';


}
$product_list = implode( ',', $prodct_list_name );

echo $product_list;

$args = array (
  'apiKey' => $API_KEY,
  'booking' => 
  array (
   //this is the part of the array where I insert my $product_list array object
    'items' => 
    array ( $product_list 
    ),
    'pickupDetail' => 
    array (
      'address' => $Pickup_Address,
    ),
    'dropoffDetail' => 
    array (
      'name' => $Shipping_Name,
      'phone' => $Phone_Number,
      'email' => $Shipping_Email,
      'description' => $Order_Notes,
      'address' => $Shipping_Address,
    ),
  ),
);
//display the json
    $json_data =  json_encode($args);
    echo $json_data;

最后json如下所示。请查看json并查看除$ product_list部分之外整个数组是如何正确编码的。我真的很喜欢,如果有办法解决这个问题或完全不同的话。

{"apiKey":API_KEY,"booking":{"items":["[0]=> array ('quantity' => 1,'sku' => CAN-BR,'description' => Candle | Black Raspberry'price' => 0),[1]=> array ('quantity' => 1,'sku' => ,'description' => Candle | Fig & Melon'price' => 21.95),"],"pickupDetail":{"address":"Pickup Address"},"dropoffDetail":{"name":"Cusomter Name","phone":"5555555555","email":"test@email.com","description":"Order Notes (order_comments)","address":"Delivery Address"}}}

1 个答案:

答案 0 :(得分:0)

因为你将它用作字符串而不是数组。使用以下代码将$product_list存储为数组:

$product_list = array();
foreach( $order_item as $product ) {
    $product_name = $product['name'];
    $product_qty = $product['qty'];
    $product_id = $product['product_id'];
    $product_price = $product['line_total'];
    $product_sku = get_post_meta( $product_id, '_sku', true);

    $product_list[$items_number++] = array ('quantity' => $product_qty,'sku' => $product_sku ,'description' => $product_name, 'price' => $product_price );
}
// $product_list = implode( ',', $prodct_list_name );

// echo $product_list;

$args = array (
  'apiKey' => $API_KEY,
  'booking' => 
  array (
   //this is the part of the array where I insert my $product_list array object
    'items' => 
    array ( $product_list 
        ),
    'pickupDetail' => 
    array (
      'address' => $Pickup_Address,
      ),
    'dropoffDetail' => 
    array (
      'name' => $Shipping_Name,
      'phone' => $Phone_Number,
      'email' => $Shipping_Email,
      'description' => $Order_Notes,
      'address' => $Shipping_Address,
      ),
    ),
  );
//display the json
$json_data =  json_encode($args);
echo $json_data;