Ich habe zwei Arrays. Einer von ihnen ist ein mehrdimensionales Array, z.PHP - array_diff Ein multidimensionales Array und ein flaches Array
$products = array(
0 => array(
'product_id' => 33,
'variation_id' => 0,
'product_price' => 500.00
),
1 => array(
'product_id' => 45,
'variation_id' => 0,
'product_price' => 600.00
),
2 => array(
'product_id' => 48,
'variation_id' => 0,
'product_price' => 600.00
),
3 => array(
'product_id' => 49,
'variation_id' => 0,
'product_price' => 600.00
)
);
und ich habe eine flache Anordnung
$missingItems= array(49,33);
Ich mag würde Produkte von $ Produkte zu entfernen, wo ihre product_id
im Array missingItems
Zeichenfolge ist.
$diff = array();
foreach ($missingItems as $missingItem) {
foreach ($products as $product) {
if($missingItem != $product['product_id']){
$diff[] = $missingItem;
}
}
}
echo '<pre>';
print_r($diff);
echo '</pre>';
Wenn ich dies tue, werden alle Werte mehrmals wiederholt. z.B. wenn ich 4 Elemente in meinem ersten Array und zwei in meinem zweiten Array habe. Es gibt 8 Ergebnisse. Ich möchte nur 2 erscheinen, d. H. Diejenigen, die nicht in dem zweiten Array vorhanden sind.
Wenn ich zwei flache Arrays habe, verwende ich array_diff
, aber ich bin mir nicht sicher, wie ich es in diesem Fall verwenden soll, wo ich ein mehrdimensionales Array und ein flaches Array habe.
In Ihrer Ausgabe-Array, sollte es eine mehrdimensionale Array mit Haupttasten 1 und 2 zurückkehren? Und entferne 0 und 3? –