Допустим, у меня есть такой массив:
$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 ]
];
Теперь я хотел бы иметь возможность удалять из массива несколько значений, например:
removeElementFromArray( $cart , [ 'productID' => '11111' , 'size' => 'M' ] );
Но мой проблема в том, что я не понимаю логи c как этого добиться. Это то, что у меня есть
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;
}