0

私は配列を持っていますが、各配列アイテムを見て、一致するものが[Product Name]別の配列アイテムと一致するかどうかを確認する方法がわからないよう[Product Name]です。

以下はサンプルデータです。 たとえば、、およびarray[0]両方array[1]は、またはのようなものに対して同じ[Product Name]ですtestが異なる値を持ちます。そのような一致を見つけて、それらを何らかの方法で1つの配列に結合できるようにしたいのですが、何も上書きしないようにします。[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 に答える