简体   繁体   English

嵌套数组和json_encode

[英]nested arrays and json_encode

I'm still very new to PHP and WordPress and I'm struggling to properly json_encode an array that has multiple woocommerce order items in it. 我仍然是PHP和WordPress的新手,我正在努力正确地json_encode一个有多个woocommerce订单项的数组。 The "order items" part of the array is the part I can't figure out because the structure needs to be dynamically created based on the number of SKUs in the order (ie 1,2,3, etc). 数组的“订单商品”部分是我无法弄清楚的部分,因为结构需要根据订单中的SKU数量动态创建(即1,2,3等)。

An example of the dynamic part of the array I need to create is below. 我需要创建的数组的动态部分的示例如下。

 $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,
                 ),
           ),
     );

To do this I created the variable $product_list which gives me my array perfectly formatted, based on the amount of products. 为此,我创建了变量$ product_list,它根据产品数量为我提供了完美格式化的数组。 When I go to echo out the $product_list it looks exactly like the sample above, just all on one line. 当我回到$ product_list时,它看起来与上面的示例完全一样,只是在一行上。

So I thought would work great... however, when I go to put the $product_list inside the bigger array, and then use json_encode to convert that array into json, low an behold, my $product_list variable just doesn't convert. 所以我觉得它会工作得很好...但是,当我将$ product_list放在更大的数组中,然后使用json_encode将该数组转换为json时,请注意,我的$ product_list变量只是不转换。 The $product_list that appears in the final json is still in array format! 最终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;

And finally the json appears as below. 最后json如下所示。 Please take a look at the json and see how the entire array got encoded correctly except the $product_list part. 请查看json并查看除$ product_list部分之外整个数组是如何正确编码的。 I'd really love if there is a way to get around this or go about it entirely differently. 我真的很喜欢,如果有办法绕过这个或完全不同的方式。

{"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"}}}

Because you use it as string and not an array. 因为您将它用作字符串而不是数组。 Use following code to store $product_list as an array: 使用以下代码将$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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM