PHP - 添加对象到子数组

PHP - add object into subarray

我有包含订单行的传入数据,订单可能不止一行。我需要对此进行解析并将其转换为带有线条子数组的单个对象。

所以我正在做的是循环遍历订单行,在第一次出现 "order ID" 我创建和对象时,然后在每次循环遍历数据时检查我是否 "order ID" 存在,如果存在,那么我想将数据添加到子数组。因为我不能确定订单 ID 可能不会在另一个字段中重复(它只是一个 int),所以我创建了一个唯一标识符来匹配。

             foreach ($orderList as $result) {
                // create the unique identifier
                $system_check = md5($result['order_id'] . $result['email']);

                // create a sting to search in, "in_array" not working
                $str = json_encode($parsedOrders);

                // Check if this is this order is already in the parsedOrder onject
                if (strpos($str, $system_check) !== false) {

                    // debug logging
                    $this->ICLogger("info", "parsed order line:", json_encode($parsedOrders[$result['order_id']]['lines']));

                    // trying to array_push the data into the "lines" section
                    $parsedOrders[$result['order_id']]['lines'][] = array(
                        'id' => $result['line_id'],
                        'product_id' => $result['product_id'],
                        'product_variant_id' => $result['product_variant_id'],
                        'quantity' => $result['quantity'],
                        'price' => $result['price']
                    );
                } else {

                    // Create the order "header" and "lines" data
                    $parsedOrders[$result['order_id']] = array
                    (
                        'id' => $result['order_id'],
                        'search_term' => $system_check,
                        'customer' => array(
                            'id' => $result['customer_id'],
                            'email_address' => $result['email'],
                            'opt_in_status' => ((int)$result['newsletter'] === 1)
                        ),
                        'fulfillment_status' => (!empty($order['fulfillment_status']) ? $order['fulfillment_status'] : ''),
                        'financial_status' => (!empty($order['financial_status']) ? $order['financial_status'] : ''),
                        'landing_site' => (!empty($order['landing_site']) ? $order['landing_site'] : ''),
                        'currency_code' => $result['currency_code'],
                        'order_total' => $result['order_total'],
                        'tax_total' => $result['tax_total'],
                        'storeid' => $result['storeid'], // temp storage of store ID
                        'lines' => array(array(
                            'id' => $result['line_id'],
                            'product_id' => $result['product_id'],
                            'product_variant_id' => $result['product_variant_id'],
                            'quantity' => (int)$result['quantity'],
                            'price' => $result['price']
                        ))
                    );
                }

当我这样做时,数据只是添加到数组的末尾,而不是在我要添加到的对象中:

JSON编码输出:

    [
      {
        "id":"20007",
        "search_term":"2d8a9bbbbc85d4503b8581bb09d81301",
        "customer":{
        "id":"11",
        "email_address":"test103@test.com",
        "opt_in_status":false
      },
        "fulfillment_status":"",
        "financial_status":"",
        "landing_site":"",
        "currency_code":"GBP",
        "order_total":"4",
        "tax_total":"1.3199999999999998",
        "storeid":"1",
        "lines":[
          {
            "id":"13",
            "product_id":"123",
            "product_variant_id":"750",
            "quantity":1,
            "price":"1"
          }
        ]
      },
      {
        "lines":[
          {
            "id":"14",
            "product_id":"123",
            "product_variant_id":"752",
            "quantity":"1",
            "price":"1"
          }
        ]
      }
    ]

如何让 "lines" 附加到 "lines" 数组而不只是附加到对象的末尾?

非常感谢...

您应该在 foreach 循环之外创建 parsedOrders 数组。然后您可以按如下方式附加对象:

<?php

$parsedOrders = [
    'yourdata' => '...',
    'lines' => [],
];

foreach ($orderList as $order) {
    $order = 'yourdata';
    $parsedOrders['lines'][] = $order; // append the order
}

你快到了你只是对你的括号感到困惑我建议将它简化为更像

$parsedOrders = [];
foreach ($orderList as $result) {

    if(isset($parsedOrders[$result['order_id']]))
    {
        //grab the existing order
        $order = $parsedOrders[$result['order_id']];
    }
    else
    {
        //if order doesn't exist create
        $order = [
            'id' => $result['order_id'],
            'search_term' => $system_check,
            'customer' => [
                'id' => $result['customer_id'],
                'email_address' => $result['email'],
                'opt_in_status' => ((int)$result['newsletter'] === 1)
            ],
            'fulfillment_status' => (!empty($order['fulfillment_status']) ? $order['fulfillment_status'] : ''),
            'financial_status' => (!empty($order['financial_status']) ? $order['financial_status'] : ''),
            'landing_site' => (!empty($order['landing_site']) ? $order['landing_site'] : ''),
            'currency_code' => $result['currency_code'],
            'order_total' => $result['order_total'],
            'tax_total' => $result['tax_total'],
            'storeid' => $result['storeid'], // temp storage of store ID
            'lines' => []
        ];

    }

    $order['lines'][$result['line_id']] = [
        'id' => $result['line_id'],
        'product_id' => $result['product_id'],
        'product_variant_id' => $result['product_variant_id'],
        'quantity' => (int)$result['quantity'],
        'price' => $result['price']
    ]

     $parsedOrders[$result['order_id']] = $order;
}