search

Home  >  Q&A  >  body text

Insert array into multidimensional structure

I have an array containing common activity_code. I need to add pri record

in project field
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
                )
        )
)
There should be 2 arrays in the

items field, but I only inserted 1 array.

Here is the code, what I have tried.

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

This is the array structure I need

Can anyone tell me what mistake I'm making here?

P粉511757848P粉511757848281 days ago467

reply all(2)I'll reply

  • P粉642919823

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

    You are overriding $purchaseArray[$purchaseRequisition['activity_code']] instead of pushing new elements into it. To push into an array, assign to $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;
    }

    reply
    0
  • P粉724256860

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

    Use the following code:

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

    reply
    0
  • Cancelreply