1

I have a single, multidimensional array that has multiple parts. Some of them are duplicates such that there are 2 (or more) different entries for the same part, most of the data is the same, but there are different pricing options, it looks lie this:

Array
(
    [0] => Array
        (
            [number] => 1234
            [description] => Part Name 1
            [e_price] => 14.40
        )

    [1] => Array
        (
            [number] => 1234
            [description] => Part Name 1
            [s_price] => 17.10
        )

    [2] => Array
        (
            [number] => 9876
            [description] => Part Name 2
            [b_price] => 26.10
        )

    [3] => Array
        (
            [number] => 9876
            [description] => Part Name 2
            [e_price] => 152.10
        )

    [4] => Array
        (
            [number] => 9876
            [description] => Part Name 2
            [s_price] => 179.10
        )

    [5] => Array
        (
            [number] => 2525
            [description] => Part Name 3
            [b_price] => 26.10
        )

    [6] => Array
        (
            [number] => 2525
            [description] => Part Name 3
            [e_price] => 152.10
        )

    [7] => Array
        (
            [number] => 2525
            [description] => Part Name 3
            [s_price] => 179.10
        )

I want to combine all the items with the same part number into a single entry, and end up with the following results:

Array
(
    [0] => Array
        (
            [number] => 1234
            [description] => Part Name 1
            [e_price] => 14.40
            [s_price] => 17.10
        )

    [1] => Array
        (
            [number] => 9876
            [description] => Part Name 2
            [b_price] => 26.10
            [e_price] => 152.10
            [s_price] => 179.10
        )

    [2] => Array
        (
            [number] => 2525
            [description] => Part Name 3
            [b_price] => 26.10
            [e_price] => 152.10
            [s_price] => 179.10
        )

I've tried using array_merge and array_unique, but I can't figure out how to merge them based on the part number when it's all in a single array to begin with.

C. Hill
  • 13
  • 5
  • 4
    Please show what you have attempted (post your code) – Ken Lee Aug 27 '23 at 04:47
  • Will there ever be a duplicate `s_price`, `b_price`, or `e_price` key for a given `number`? If so, what is the expected behavior of the merge? – kmoser Aug 27 '23 at 04:50
  • [`array_map`](https://www.php.net/manual/en/function.array-map.php) has a callback parameter that lets you to do logic into the array elements. – Bagus Tesa Aug 27 '23 at 05:44
  • 1
    Possible duplicate of https://stackoverflow.com/questions/13669554/append-column-values-from-a-2d-array-to-the-rows-of-another-2d-array-based-on-sh – nice_dev Aug 27 '23 at 06:02
  • @BagusTesa `array_map()` is not useful for merging array elements. Its output is always a 1-to-1 correspondence with the input. – Barmar Aug 27 '23 at 06:23
  • 1
    The general approach to grouping problems like this is to make an associative array whose keys are the part numbers. Then you can merge the elements with the same part number. – Barmar Aug 27 '23 at 06:25
  • Does this answer your question? [Append column values from a 2d array to the rows of another 2d array based on shared column values](https://stackoverflow.com/questions/13669554/append-column-values-from-a-2d-array-to-the-rows-of-another-2d-array-based-on-sh) – OMi Shah Aug 27 '23 at 07:28

1 Answers1

0

You could achieve it like this:

$originalArray = [
    ['number' => 1234, 'description' => 'Part Name 1', 'e_price' => 14.40],
    ['number' => 1234, 'description' => 'Part Name 1', 's_price' => 17.10],
    ['number' => 9876, 'description' => 'Part Name 2', 'b_price' => 26.10],
    ['number' => 9876, 'description' => 'Part Name 2', 'e_price' => 152.10],
    ['number' => 9876, 'description' => 'Part Name 2', 's_price' => 179.10],
    ['number' => 2525, 'description' => 'Part Name 3', 'b_price' => 26.10],
    ['number' => 2525, 'description' => 'Part Name 3', 'e_price' => 152.10],
    ['number' => 2525, 'description' => 'Part Name 3', 's_price' => 179.10]
];

$combined = [];

foreach ($originalArray as $part) {
    $partNumber = $part['number'];
    
    if (!isset($combined[$partNumber])) {
        $combined[$partNumber] = [
            'number' => $partNumber,
            'description' => $part['description']
        ];
    }

    // Merge pricing options
    unset($part['number'], $part['description']);  // remove these keys to avoid overwriting
    $combined[$partNumber] = array_merge($combined[$partNumber], $part);
}

// Reset the keys to be sequential
$combined = array_values($combined);

print_r($combined);

Outpus is :

Array
(
    [0] => Array
        (
            [number] => 1234
            [description] => Part Name 1
            [e_price] => 14.4
            [s_price] => 17.1
        )

    [1] => Array
        (
            [number] => 9876
            [description] => Part Name 2
            [b_price] => 26.1
            [e_price] => 152.1
            [s_price] => 179.1
        )

    [2] => Array
        (
            [number] => 2525
            [description] => Part Name 3
            [b_price] => 26.1
            [e_price] => 152.1
            [s_price] => 179.1
        )

)
MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
  • 1
    This worked perfectly! I had not thought of building a new array with the keys as the part number. – C. Hill Aug 29 '23 at 23:31