1

This is my data:

$dataItems = [
    [
        'id' => 1,
        'serial' => "XXXXXXAA1",
        'pd_item_info' =>
        [
            'id' => 1,
            'quantity' => 5,
            'ipo_item_pml_info' => 
            [
                'id' => 1,
                'product_name' => 'Keyboard'
            ]
        ]
    ], 
    [
        'id' => 2,
        'serial' => "XXXXXXAA2",
        'pd_item_info' =>
        [
            'id' => 2,
            'quantity' => 10,
            'ipo_item_pml_info' => 
            [
                'id' => 2,
                'product_name' => 'Keyboard'
            ]
        ]
    ]
];

and I need to have this output:

$output = [
    'product_name' => "Keyboard",
    'serial' => ["XXXXXXAA1", "XXXXXXAA2"]
];

Using foreach and array_push in Laravel, how can I merge duplicate product_name values and join their serial values?

2 Answers 2

1

i assume pd_item_info and ipo_item_pml_info always there

first distiguish the array key based on product name and all the serial key is append under same product name

foreach($dataItems as $item) {
    $products[$item['pd_item_info']['ipo_item_pml_info']['product_name']][] = $item['serial'];
}

//output
// [
//     'Keyboard' => ['XXXXXXAA1', 'XXXXXXAA2'];
// ]

next, formatting according to your needed

foreach($products as $key => $product) {
    $output[] = [
        'product_name' => $key,
        'serial' => $product
    ];
}

Thats it.. =D

Sign up to request clarification or add additional context in comments.

Comments

0

You can use array destructuring syntax to only access the data points which you require and populate your result array via a body-less loop.

Because you have a 1-to-many relationship, just create an associative array where the keys are the product names and the values are subarrays of serial numbers. Demo

(I've extended your input data a little.)

$result = [];
foreach (
    $dataItems as [
        'pd_item_info' => ['ipo_item_pml_info' => ['product_name' => $name]],
        'serial' => $result[$name][]
    ]
);
var_export($result);

Output:

array (
  'Keyboard' => 
  array (
    0 => 'XXXXXXAA1',
    1 => 'XXXXXXAA2',
  ),
  'Speaker' => 
  array (
    0 => 'XXXXXXFOO',
  ),
)

If you want the indexed 3d array as requested in the question body and wish to use Laravel's collection methods, here's that approach. PHPize Demo

var_export(
    $dataItems
    ->groupBy(fn($item) => $item['pd_item_info']['ipo_item_pml_info']['product_name'])
    ->map(fn($group, $key) => [
        'product_name' => $key,
        'serial' => $group->pluck('serial')->toArray()
    ])
    ->values()
    ->toArray()
);

Output:

array (
  0 => 
  array (
    'product_name' => 'Keyboard',
    'serial' => 
    array (
      0 => 'XXXXXXAA1',
      1 => 'XXXXXXAA2',
    ),
  ),
  1 => 
  array (
    'product_name' => 'Speaker',
    'serial' => 
    array (
      0 => 'XXXXXXFOO',
    ),
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.