0

我有一个数组,我似乎无法弄清楚如何查看每个数组项并检查 a 是否[Product Name]与另一个数组项匹配[Product Name]

以下是样本数据。 例如,对于or之类的东西array[0]array[1]两者都有相同[Product Name]的 of但不同的值。我希望能够找到这样的匹配项并以某种方式将它们组合到一个数组中,但不会覆盖任何内容。test[Variant Name][Variant Full Stock]

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
        )
)

理想情况下,我想将上述数组转换为一个新数组,该数组将输出如下内容:

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
    )
)

有什么解决办法吗?

4

2 回答 2

3

您需要做的就是迭代数组并分组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看起来像这样:

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)
    }
  }
}

不完全是您的想法,但也许更有用一点?

于 2012-09-08T21:14:06.170 回答
2

尽管您已经有了答案,但我开始编写一些代码,所以我想完成它。

$newar=array();
$k=0;
while($ar[0])
{
    $keys=array();
    foreach($ar as $k2=>$v2)if($ar[0]['Product Name']==$ar[$k2]['Product Name']&&$k2!=0)$keys[]=$k2;
    $newar[$k]=array($ar[0]);
    unset($ar[0]);
    foreach($keys as $x)
    {
        $newar[$k][]=$ar[$x];
        unset($ar[$x]);
    }
    ++$k;
    $ar=array_values($ar);
}
$ar=$newar;

例如,这个:

Array
(
    [0] => Array
        (
            [Product Name] => test
        )

    [1] => Array
        (
            [Product Name] => test
        )

    [2] => Array
        (
            [Product Name] => another test
        )

)

会变成:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [Product Name] => test
                )

            [1] => Array
                (
                    [Product Name] => test
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [Product Name] => another test
                )

        )

)
于 2012-09-08T21:42:53.853 回答