Home > Mobile >  Insert array to the multidimensional structure
Insert array to the multidimensional structure

Time:04-23

I have an array with common activity_code. I need to add the pri records inside item 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
                )
        )
)

It is supposed to have 2 array inside items field but, I am getting only 1 array inserted.

This is the code, 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

array Structure

Can anybody tell me what mistake I have done here ?

CodePudding user response:

Use the below 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;
        
    }

CodePudding user response:

You're overwriting $purchaseArray[$purchaseRequisition['activity_code']], not pushing a new element 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;
}
  •  Tags:  
  • php
  • Related