如何在条件下删除json对象

时间:2016-10-07 12:33:06

标签: php arrays json curl

嗨大家可以告诉我如何在条件

的基础上从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
  }]

但我想删除状态为Archived的那个人的全部数据。所以任何人都可以告诉我我该怎么做。

2 个答案:

答案 0 :(得分:5)

一种简单的方法是使用array_filter

您需要先将json转换为数组:

$users = json_decode($json, true);

现在使用过滤用户数组,删除状态为Archived:

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

现在$active将是一组没有存档状态的用户。

如果你需要在json中找到结果:

$json = json_encode($active);

以下是一个工作示例:https://3v4l.org/5KMaC

答案 1 :(得分:-1)

你可以使用给定的代码,它正常工作: -

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