我有一个复杂的嵌套数组,我想删除具有特定 store_id 的所有项目及其子项:
这真的很困难,我不知道如何解决。
Array
(
[0] => Array
(
[cart_id] => 89
[product_id] => 46
[store_id] => 2
[option] => Array
(
[0] => Array
(
[product_option_id] => 92
[value] => Aqua
)
[1] => Array
(
[product_option_id] => 91
[value] => 85C
)
)
)
[1] => Array
(
[cart_id] => 90
[product_id] => 46
[store_id] => 2
)
Run Code Online (Sandbox Code Playgroud)
非常感谢您的任何帮助。
如果您想删除具有特定 store_id 的整个数组元素,则只需遍历数组,检查 store_id 并删除该元素(如果不再需要)。
例如:
<?php
foreach($data as $key=>$row){
if($row['store_id'] == 2){
unset($data[$key]);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
您可以将“2”更改为您想要专门删除商店的任何内容。或者,如果您想匹配多个商店,您可以更改 if 以匹配一组 id。