PHP - 将对象添加到子数组中

我有包含订单行的传入数据,订单可能有多于一行。我需要解析它并将其转换为带有行子数组的单个对象。

所以我正在做的是循环遍历订单行,在我创建和对象的“订单 ID”第一次出现时,然后在通过数据的每个循环中检查我是否存在“订单 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":"[email protected]",
        "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"
          }
        ]
      }
    ]

如何将“行”附加到“行”数组而不是仅附加到对象的末尾?

非常感谢...

迈克

你快到了,你刚刚对你的括号感到困惑,我建议把它简化成更像

$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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用PHP将对象添加到JSON数组

来自分类Dev

使用PHP将对象添加到JSON数组

来自分类Dev

将值添加到对象PHP中的数组

来自分类Dev

PHP:添加到数组中的数组

来自分类Dev

添加到PHP中的多维数组

来自分类Dev

在php中将数组添加到子数组

来自分类Dev

将数组对象添加到数组php?

来自分类Dev

如何在php中将元素添加到子数组

来自分类Dev

如何将子节点添加到数组 PHP

来自分类Dev

PHP 或 jquery 代码将对象照片添加到另一个对象照片?

来自分类Dev

如何将json格式的fabricjs-canvas对象添加到PHP中的索引数组?

来自分类Dev

如何通过PHP将JSON对象数组添加到MySQL中

来自分类Dev

如何使用 JSON 和 PHP 在表单提交时将我的对象添加到数组中?

来自分类Dev

将数组元素添加到PHP中的数组

来自分类Dev

如果在php中满足条件,则在foreach循环中将数组对象添加到数组中

来自分类Dev

将变量添加到PHP中的函数中的数组

来自分类Dev

将PHP中的数组添加到mysql中

来自分类Dev

将对象添加到预分配的对象数组中

来自分类Dev

将对象添加到预分配的对象数组中

来自分类Dev

使用useReducer将对象添加到数组中的数组

来自分类Dev

使用redux将对象添加到数组中的数组

来自分类Dev

在PHP中将函数添加到数组中

来自分类Dev

在PHP中将重复值添加到数组中

来自分类Dev

值未添加到PHP foreach语句的数组中

来自分类Dev

将字段 - 值添加到 php 中的多维数组

来自分类Dev

如何将数组添加到 PHP 中的 cookie?

来自分类Dev

将对象添加到对象数组

来自分类Dev

将对象添加到对象数组

来自分类Dev

将对象添加到构造函数中的数组

Related 相关文章

热门标签

归档