cari

Rumah  >  Soal Jawab  >  teks badan

Masukkan tatasusunan ke dalam struktur berbilang dimensi

Saya mempunyai senarai yang mengandungi activity_code 的数组。我需要在项目字段中添加 pri rekod biasa

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 Perlu ada 2 tatasusunan dalam medan, tetapi saya hanya memasukkan 1 tatasusunan.

Ini adalah kod, apa yang saya telah cuba.

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

Ini adalah struktur tatasusunan yang saya perlukan

Boleh sesiapa beritahu saya apa kesilapan yang saya lakukan di sini?

P粉511757848P粉511757848285 hari yang lalu474

membalas semua(2)saya akan balas

  • P粉642919823

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

    Anda sedang bertudung $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;
    }

    balas
    0
  • P粉724256860

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

    Gunakan kod di bawah:

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

    balas
    0
  • Batalbalas