我有一个数组,我似乎无法弄清楚如何查看每个数组项并检查[Product Name]
是否与另一个数组项[Product Name]
匹配?
以下是示例数据。 例如,array[0]
和array[1]
两者都具有相同的[Product Name]
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
)
)
任何解决方案?
答案 0 :(得分: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)
}
}
}
不完全是你的想法,但也许更有用?
答案 1 :(得分: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
)
)
)