2016-10-07 69 views
0

嗨,大家好誰能告訴我如何刪除的條件如何在條件刪除JSON對象

的基礎上,從JSON對象中的數據我有這樣的數據

[ 
    { 
    "username": "7291991451", 
    "uid": "mfwUrskyhzVvzS16pS_a", 
    "name": "Abhishek B", 
    "status": "Active", 
    "is_busy": true, 
    "duty_day_bits": "1111111", 
    "duty_start_time": null, 
    "duty_end_time": null, 
    "vehicle": null, 
    "vehicle_type": null, 
    "todays_mileage": 2244, 
    "yesterdays_mileage": 30498, 
    "created_at": " 3/05/2016 5:14:12PM", 
    "merchant_address": null, 
    "merchant_id": null 
    }, 
    { 
    "username": "7291991462", 
    "uid": "G1knzKyuKoZK78CZySyA", 
    "name": "Akash Gupta", 
    "status": "Active", 
    "is_busy": false, 
    "duty_day_bits": "1111111", 
    "duty_start_time": null, 
    "duty_end_time": null, 
    "vehicle": null, 
    "vehicle_type": null, 
    "todays_mileage": 0, 
    "yesterdays_mileage": 0, 
    "created_at": "18/05/2016 1:16:19PM", 
    "merchant_address": null, 
    "merchant_id": null 
    }, 
    { 
    "username": "7291991456", 
    "uid": "ndHsmz-BvyXfjv42MTyd", 
    "name": "Ankur Sagar", 
    "status": "Archived", 
    "is_busy": false, 
    "duty_day_bits": "1111111", 
    "duty_start_time": null, 
    "duty_end_time": null, 
    "vehicle": null, 
    "vehicle_type": null, 
    "todays_mileage": 0, 
    "yesterdays_mileage": 0, 
    "created_at": " 3/05/2016 5:36:00PM", 
    "merchant_address": null, 
    "merchant_id": null 
    }, 
    { 
    "username": "7503710039", 
    "uid": "j-w2jxx14s6GgF_YkcFP", 
    "name": "Annu Gupta", 
    "status": "Archived", 
    "is_busy": false, 
    "duty_day_bits": "1111111", 
    "duty_start_time": null, 
    "duty_end_time": null, 
    "vehicle": null, 
    "vehicle_type": null, 
    "todays_mileage": 0, 
    "yesterdays_mileage": 0, 
    "created_at": " 2/09/2016 12:59:13PM", 
    "merchant_address": null, 
    "merchant_id": null 
    }, 
    { 
    "username": "9599380369", 
    "uid": "KarAFisqeRpcr_xtEhEB", 
    "name": "Arun Kumar", 
    "status": "Active", 
    "is_busy": true, 
    "duty_day_bits": "1111111", 
    "duty_start_time": null, 
    "duty_end_time": null, 
    "vehicle": null, 
    "vehicle_type": null, 
    "todays_mileage": 0, 
    "yesterdays_mileage": 0, 
    "created_at": " 5/05/2016 1:30:33PM", 
    "merchant_address": null, 
    "merchant_id": null 
    }] 

,但我想刪除整個數據的狀態是存檔的那個傢伙。那麼,任何人都可以告訴我,我該如何做到這一點。

+0

'json_decode ' - >'loop' - > if(arhcived)添加到新數組中。 - >'json_encode' –

+0

@DivyanshuKumar你看到我的答案在下面了嗎?它有幫助嗎? – jszobody

回答

5

一個簡單的方法就是使用array_filter

你需要先把你的JSON轉換爲數組:

$users = json_decode($json, true); 

現在使用過濾器的用戶的數組,存檔的狀態刪除那些:

$active = array_filter($users, function($user) { 
    return $user['status'] != 'Archived'; 
}); 

現在$active會是一個沒有存檔狀態的用戶數組。

如果您需要的結果返回到JSON:

$json = json_encode($active); 

這裏有一個工作示例:https://3v4l.org/5KMaC

+0

嗨在這種方法中我隨機獲取數組編號,我必須將數據加載到下拉列表中。那我能怎麼做,你能告訴我嗎? –

+0

然後你在代碼中的某處出現了不同的問題。我提供的答案完全適用於您提供的JSON。您可能需要顯示您的PHP代碼,以便我們可以瞭解您正在做什麼。可能最好發佈一個新問題,因爲這是一個不同的問題。 – jszobody

-1

您可以使用給定的代碼,它工作正常: -

$users = json_decode($json, true); 
    $count=count($users); 
    for($i=0; $i<$count; $i++) 
    { 
     if($users->status=="Archived") 
     { 
     print_r($users); 
     } 
    }