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?

Answers


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?


Need Your Help

Why isn't is the drop down menu working

javascript html css twitter-bootstrap twitter-bootstrap-3

I wanted to know why isn't the drop down menu working. When I click on the drop down menu it does not appear. I am using bootstrap. Please tell me why it isn't working.

What is the correct way to make SQLalchemy store strings as lowercase?

python sqlite sqlalchemy case-insensitive

I'm using SQLAlchemy to talk to my database. Because not many people will be using my application (at least initially), I figure SQLite is the quickest/easiest back end.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.