2017-12-17 12 views
-1

1つのJSON配列があります。 dispatch_quantity = 0の要素を削除します。PHP:条件に基づいてJSON配列から要素を削除する

[{ 
"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]); 
    } 
}  
+2

あなたの問題は何?何を試しましたか? –

+0

私はこれを試しました $ i = 0; foreach($ data as $ json_array){ $ dispatch_quantity = $ json_array ['dispatch_quantity']; if($ dispatch_quantity == 0){ unset($ json_array [$ i]); } $ 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]); 
    } 
} 
+0

しかし、それは完全な配列を削除しています。私はprint_rを使用して印刷しています アレイ ( ) –

+0

Nop、dispatch_quantity == 0のみ、私はローカルでテストしました。 –

+0

$ order-> dispatch_quantityを出力した場合、if条件から出力配列を取得しています。すなわち、再び配列ですか? –

関連する問題