Q:

Look for matches in PHP array then combine the array without any overwriting

I have an array and I can't seem to figure out how to look at each array item and check if a [Product Name] matches another array item's [Product Name] or not?
Below is sample data. For example, array[0] and array[1] both have the same [Product Name] of test but different values for things like [Variant Name] or [Variant Full Stock]. I want to be able to find matches like that and combine those somehow to one array but not overwrite anything.
Array (
    [0] => Array (
            [Product Name] => test
            [Variant Name] => Default
            [Variant SKU] => 
            [Variant In Stock] => 1
            [Variant Full Stock] => 1
            [id] => 0
        )
    [1] => Array (
            [Product Name] => test
            [Variant Name] => testingVar
            [Variant SKU] => 
            [Variant In Stock] => 1
            [Variant Full Stock] => 2
            [id] => 1
        )
    [2] => Array (
            [Product Name] => another test
            [Variant Name] => Default
            [Variant SKU] => 
            [Variant In Stock] => 1
            [Variant Full Stock] => 1
            [id] => 2
        )
)

Ideally, I would want to convert the above array to a new array that would output something like this:
Array (
    [0] => Array (
        [0] => Array (
            [Product Name] => test
            [Variant Name] => Default
            [Variant SKU] => 
            [Variant In Stock] => 1
            [Variant Full Stock] => 1
            [id] => 0
        )
        [1] => Array (
            [Product Name] => test
            [Variant Name] => testingVar
            [Variant SKU] => 
            [Variant In Stock] => 1
            [Variant Full Stock] => 2
            [id] => 1
        )
    )
    [1] => Array (
        [Product Name] => another test
        [Variant Name] => Default
        [Variant SKU] => 
        [Variant In Stock] => 1
        [Variant Full Stock] => 1
        [id] => 2
    )
)

Any solutions?

A:

All you need to do is iterate the array and group by Product Name
$data =
    array(
        array(
            "Product Name" => "test",
            "Variant Name" => "Default",
            "Variant SKU" => "",
            "Variant In Stock" => 1,
            "Variant Full Stock" => 1,
            "id" => 0
        ),
        array(
            "Product Name" => "test",
            "Variant Name" => "testingVar",
            "Variant SKU" => "",
            "Variant In Stock" => 1,
            "Variant Full Stock" => 2,
            "id" => 1
        ),
        array(
            "Product Name" => "another test",
            "Variant Name" => "testingVar",
            "Variant SKU" => "",
            "Variant In Stock" => 1,
            "Variant Full Stock" => 2,
            "id" => 2
        ),
    );

$result = array();

foreach($data as $item) {
    $key = $item["Product Name"];
    $result[$key][] = $item;
}

$result looks like this: 
array(2) {
  ["test"]=>
  array(2) {
    [0]=>
    array(6) {
      ["Product Name"]=>
      string(4) "test"
      ["Variant Name"]=>
      string(7) "Default"
      ["Variant SKU"]=>
      string(0) ""
      ["Variant In Stock"]=>
      int(1)
      ["Variant Full Stock"]=>
      int(1)
      ["id"]=>
      int(0)
    }
    [1]=>
    array(6) {
      ["Product Name"]=>
      string(4) "test"
      ["Variant Name"]=>
      string(10) "testingVar"
      ["Variant SKU"]=>
      string(0) ""
      ["Variant In Stock"]=>
      int(1)
      ["Variant Full Stock"]=>
      int(2)
      ["id"]=>
      int(1)
    }
  }
  ["another test"]=>
  array(1) {
    [0]=>
    array(6) {
      ["Product Name"]=>
      string(12) "another test"
      ["Variant Name"]=>
      string(10) "testingVar"
      ["Variant SKU"]=>
      string(0) ""
      ["Variant In Stock"]=>
      int(1)
      ["Variant Full Stock"]=>
      int(2)
      ["id"]=>
      int(2)
    }
  }
}

Not exactly what you had in mind, but perhaps a bit more usable?

