我有一个JSON数组。我想删除一个dispatch_quantity = 0的元素。
[{
"order_no": "ORDER123",
"dispatch_quantity": "500",
"balance_quantity": "500"
}, {
"order_no": "ORDER123",
"dispatch_quantity": "0",
"balance_quantity": "500"}]
操作完成后,输出应为:
[{
"order_no": "ORDER123",
"dispatch_quantity": "500",
"balance_quantity": "500",
}]
我试过了:
foreach ($data as $json_array) {
$dispatch_quantity = $json_array['dispatch_quantity'];
if ($dispatch_quantity == 0) {
unset($json_array[$i]);
}
}
答案 0 :(得分:2)
$json ='[{
"order_no": "ORDER123",
"dispatch_quantity": "500",
"balance_quantity": "500"
}, {
"order_no": "ORDER123",
"dispatch_quantity": "0",
"balance_quantity": "500"
}]';
$orders = json_decode($json);
$filteredOrders = $orders;
foreach ($filteredOrders as $key => $order)
{
if ($order->dispatch_quantity == 0)
{
unset($filteredOrders[$key]);
}
}