將數組插入多維結構中
P粉511757848
P粉511757848 2024-02-17 21:35:08
0
2
457

我有一個包含常見 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粉511757848
P粉511757848

全部回覆(2)
P粉642919823

您正在覆寫 $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;
}
P粉724256860

使用下面的程式碼:

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;
        
    }
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板