这是我的数组:
$productsArray = [
'0' => [
'id' => 1,
'code' => 1000,
'in_promo' => 1,
],
'1' => [
'id' => 2,
'code' => 1000,
'in_promo' => 3,
],
];
我需要检查是否存在具有相同'code'
和'in_promo' => 1
和3
的元素,并删除具有'in_promo' => 1
的元素,以支持具有'in_promo' => 3
的元素。如何用PHP数组函数做到这一点?
谢谢。
$productsArray = [
'0' => [
'id' => 1,
'code' => 1000,
'in_promo' => 1,
],
'1' => [
'id' => 2,
'code' => 1000,
'in_promo' => 3,
],
'10' => [
'id' => 3,
'code' => 1000,
'in_promo' => 2,
],
'11' => [
'id' => 4,
'code' => 1001,
'in_promo' => 3,
],
];
$hasPromoNeedle = array_filter($productsArray, function (array $array)
{
return in_array($array['in_promo'], [1, 3]);
});
print_r(array_filter($hasPromoNeedle, function (array $array1, $k1) use ($hasPromoNeedle) {
foreach ($hasPromoNeedle as $k2 => $array2)
{
if ($array1['code'] == $array2['code'] and $k1 != $k2)
{
return true;
}
}
return false;
}, ARRAY_FILTER_USE_BOTH ));