V
V
Vasily Morozov2021-08-05 18:03:58
PHP
Vasily Morozov, 2021-08-05 18:03:58

How to leave elements with a unique key value in a multidimensional array?

Hello!
Task:
There is an initial multidimensional array (I am attaching part of it):

$array = array (
  'accessories' => 
  array (    
    array (
      'id' => 11,
      'accessory_group_id' => 6,
      'title' => 'Добор 2050*110*16',
      'vendor_code' => '025-0079',
    ),    
    array (
      'id' => 769,
      'accessory_group_id' => 6,
      'title' => 'Карниз Тип-3 120 см',
      'vendor_code' => '025-0123',
    ),    
    array (
      'id' => 768,
      'accessory_group_id' => 12,
      'title' => 'Добор 2050*110*16',
      'vendor_code' => '025-0079',
    ),    
    array (
      'id' => 3464,
      'accessory_group_id' => 6,
      'title' => 'Карниз Тип-3 70 см',
      'vendor_code' => '025-0120',
    ),    
    array (
      'id' => 3465,
      'accessory_group_id' => 6,
      'title' => 'Добор 2050*110*16',
      'vendor_code' => '025-0079',
    ),    
    array (
      'id' => 3466,
      'accessory_group_id' => 6,
      'title' => 'Карниз Тип-3 90 см',
      'vendor_code' => '025-0122',
    ),
  ),
);


The elements of the 'accessories' array have the key 'vendor_code' and its value is repeated in some elements (in this example, these are elements with indices 0, 2 and 4) - let's conditionally call these elements "same". In these "identical" elements, the key value 'accessory_group_id' can be repeated (elements with indexes 0 and 4).
The essence of the question is this:
how to leave in the original array only elements with a unique value of the 'vendor_code' key, while adding unique values ​​from all "identical" elements separated by commas to the 'accessory_group_id' key?
An example of the desired result:

$result_array = array (
  'accessories' => 
  array (    
    array (
      'id' => 11,
      'accessory_group_id' => '6,12'
      'title' => 'Добор 2050*110*16',
      'vendor_code' => '025-0079',
    ),    
    array (
      'id' => 769,
      'accessory_group_id' => 6,
      'title' => 'Карниз Тип-3 120 см',
      'vendor_code' => '025-0123',
    ), 
    array (
      'id' => 3464,
      'accessory_group_id' => 6,
      'title' => 'Карниз Тип-3 70 см',
      'vendor_code' => '025-0120',
    ), 
    array (
      'id' => 3466,
      'accessory_group_id' => 6,
      'title' => 'Карниз Тип-3 90 см',
      'vendor_code' => '025-0122',
    ),
  ),
);


Tell me, please, how to do it? Any help will do: an algorithm, pseudocode, or ready-made code...)
Thank you all in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Hog, 2021-08-05
@vasmor

My barbaric decision in the forehead))

$array = [
    'accessories' => [ /* ... Ваш массив ... */],
];


$tmp = [];

foreach ($array['accessories'] as $item) {
    if (!isset($tmp[$item['vendor_code']])) {
        $tmp[$item['vendor_code']] = $item;
        continue;
    }

    $tmp[$item['vendor_code']]['accessory_group_id'] = implode(',',
        array_unique(
            explode(',', $tmp[$item['vendor_code']]['accessory_group_id'] . ',' . $item['accessory_group_id'])
        )
    );
}

$result_array['accessories'] = array_values($tmp);

var_dump($result_array);

L
Lander, 2021-08-05
@usdglander

$result_array = [
    'accessories' => array_values(array_combine(
        array_column($array['accessories'], 'vendor_code'),
        $array['accessories']
    ))
];

Will it do so?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question