首页  >  问答  >  正文

将数组插入多维结构中

我有一个包含常见 activity_code 的数组。我需要在项目字段中添加 pri 记录

Array
(
    [0] => Array
        (
            [pr] => Array
                (
                    [id] => 1
                    [activity_code] => 20220101PR0001
                    [serial_number] => 0001/2022
                )
            [pri] => Array
                (
                    [item] => Item 1
                    [description] => Description 1
                    [quantity] => 15
                    [unit] => Each
                    [unit_price] => 65000.00
                )
        )

    [1] => Array
        (
            [pr] => Array
                (
                    [id] => 1
                    [activity_code] => 20220101PR0001
                    [serial_number] => 0001/2022
                )

            [pri] => Array
                (
                    [item] => Item 2
                    [description] => Description 2
                    [quantity] => 15
                    [unit] => Each
                    [unit_price] => 2500.00
                )
        )
)

items 字段中应该有 2 个数组,但是我只插入了 1 个数组。

这是代码,我已经尝试过。

foreach ($records as $key => $record) {
        $purchaseRequisition = $record['pr'];
        $items = $record['pri'];

        if (!array_key_exists($purchaseRequisition['activity_code'], $purchaseArray)) {
            $purchaseArray[$purchaseRequisition['activity_code']] = [];
        }
        $purchaseArray[$purchaseRequisition['activity_code']] = [
            'serialNumber' => $record['pr']['serial_number'],
            'items' => []
        ];

        $purchaseArray[$purchaseRequisition['activity_code']]['items'][$key] = $items;
    }

这是我需要的数组结构

谁能告诉我我在这里犯了什么错误?

P粉511757848P粉511757848268 天前452

全部回复(2)我来回复

  • P粉642919823

    P粉6429198232024-02-18 16:25:14

    您正在覆盖 $purchaseArray[$purchaseRequisition['activity_code']],而不是向其中推送新元素。要推入数组,请分配给 $array[]

    foreach ($records as $key => $record) {
        $purchaseRequisition = $record['pr'];
        $items = $record['pri'];
    
        if (!array_key_exists($purchaseRequisition['activity_code'], $purchaseArray)) {
            $purchaseArray[$purchaseRequisition['activity_code']] = [];
        }
        $purchaseArray[$purchaseRequisition['activity_code']][] = [
            'serialNumber' => $record['pr']['serial_number'],
            'items' => []
        ];
    
        $purchaseArray[$purchaseRequisition['activity_code']]['items'][$key] = $items;
    }

    回复
    0
  • P粉724256860

    P粉7242568602024-02-18 15:58:30

    使用下面的代码:

    foreach ($records as $key => $record) {
            $purchaseRequisition = $record['pr'];
            $items[] = $record['pri'];
    
            if (!array_key_exists($purchaseRequisition['activity_code'], $purchaseArray)) {
                $purchaseArray[$purchaseRequisition['activity_code']] = [];
            
            }
            $purchaseArray[$purchaseRequisition['activity_code']] = [
                'serialNumber' => $record['pr']['serial_number'],
                'items' => []
            ];
    
    
            $purchaseArray[$purchaseRequisition['activity_code']]['items'] = $items;
            
        }

    回复
    0
  • 取消回复