Let's say I have an array like this one:
$cart = [
[ 'productID' => '11111' , 'size' => 'M' , 'quantity' => 2 ],
[ 'productID' => '11111' , 'size' => 'L' , 'quantity' => 4 ],
[ 'productID' => '22222' , 'size' => 'S' , 'quantity' => 3 ],
[ 'productID' => '22222' , 'size' => 'L' , 'quantity' => 7 ],
[ 'productID' => '33333' , 'size' => 'M' , 'quantity' => 1 ]
];
Now I would like to be able to delete from array by multiple values like this:
removeElementFromArray( $cart , [ 'productID' => '11111' , 'size' => 'M' ] );
But my problem is I do not understand the logic how to achieve this. This is what I have
function removeElementFromArray($targetArray=[],$needles=[]){
foreach( $targetArray as $subKey => $subArray ){
// This is wrong because $removeIt becomes TRUE by any needle matched
// but I want it to become TRUE only if all $needles match.
foreach( $needles as $key => $value ){
if( $subArray[$key] == $value ){
$removeIt = TRUE;
}
}
// delete row from array
if( $removeIt == TRUE ){ unset($targetArray[$subKey]); }
}
// return
return $targetArray;
}