Group data in a multidimensional array based on two columns



PHP Snippet 1:

<?php
    // Test data.
    $sourceArray = array(array('brand'=>'ABC', 'model'=>'xyz', 'size'=>13),
                         array('brand'=>'QWE', 'model'=>'poi', 'size'=>23),
                         array('brand'=>'ABC', 'model'=>'xyz', 'size'=>18),
                        );
    $newArray = array();

    // Create a new array from the source array. 
    // We'll use the brand/model as a lookup.
    foreach($sourceArray as $element) {

        $elementKey = $element['brand'] . '_' . $element['model'];

        // Does this brand/model combo already exist?
        if(!isset($newArray[$elementKey])) {
            // No - create the new element.
            $newArray[$elementKey] = array('brand'=>$element['brand'],
                                           'model'=>$element['model'], 
                                           'size'=>array($element['size']),
                                           );
        }
        else {
            // Yes - add the size (if it's not already present).
            if(!in_array($element['size'], $newArray[$elementKey]['size'])) {
                $newArray[$elementKey]['size'][] = $element['size'];
            }
        }
    }

    // *** DEBUG ***
    print_r($newArray);
?>

PHP Snippet 2:

//$array is the array in your first example.

foreach($array as $item) {
  $itemname = $item["brand"] . "_" . $item["model"]

  $new_array[$itemname]["brand"]  = $item["brand"];
  $new_array[$itemname]["model"]  = $item["model"];
  $new_array[$itemname]["size"][] = $item["size"];
}

PHP Snippet 3:

foreach($array as $item) {
  $itemname = $item["brand"] . "_" . $item["model"]

  $new_array[$itemname]["brand"]  = $item["brand"];
  $new_array[$itemname]["model"]  = $item["model"];
  $new_array[$itemname]["size"][ $item["size"] ] = 1;
}

foreach($new_array as $itemname=>$data) {
  if(isset($data['size']) && is_array($data['size'])) {
    $new_array[$itemname]['size']=array_keys($new_array[$itemname]['size']);
  }
}

PHP Snippet 4:

$result = [];
foreach ($array as $row) {
    $compositeKey = $row['brand'] . '_' . $row['model'];
    if (!isset($result[$compositeKey])) {
        $result[$compositeKey] = $row;
    } else {
        $result[$compositeKey]['size'] = array_merge(
            (array)$result[$compositeKey]['size'],
            [$row['size']]
        );
    }
}
var_export($result);

PHP Snippet 5:

var_export(
    array_values(
        array_reduce(
            $array,
            function ($carry, $row) {
                $compositeKey = $row['brand'] . '_' . $row['model'];
                if (!isset($carry[$compositeKey])) {
                    $carry[$compositeKey] = $row;
                } else {
                    $carry[$compositeKey]['size'] = array_merge(
                        (array)$carry[$compositeKey]['size'],
                        [$row['size']]
                    );
                }
                return $carry;
            }
        )
    )
);

PHP Snippet 6:

$result = [];
foreach ($array as $row) {
    $compositeKey = $row['brand'] . '_' . $row['model'];
    if (!isset($result[$compositeKey])) {
        $row['size'] = (array)$row['size'];
        $result[$compositeKey] = $row;
    } else {
        $result[$compositeKey]['size'][] = $row['size'];
    }
}
var_export(array_values($result));