Array in mehrdimensionale Struktur einfügen
P粉511757848
P粉511757848 2024-02-17 21:35:08
0
2
463

Ich habe eine Liste mit häufigen activity_code 的数组。我需要在项目字段中添加 priDatensätzen

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 Es sollten 2 Arrays im Feld vorhanden sein, aber ich habe nur 1 Array eingefügt.

Hier ist der Code, den ich ausprobiert habe.

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

Das ist die Array-Struktur, die ich brauche

Kann mir jemand sagen, welchen Fehler ich hier mache?

P粉511757848
P粉511757848

Antworte allen(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;
        
    }
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage