简体   繁体   English

如何在条件下删除json对象

[英]how to remove json object on condition

Hi Guys can anyone tell me how to remove the data from the json object on the base of condition 嗨,大家好,有人可以告诉我如何根据条件从json对象中删除数据

I have this data 我有这个数据

[
  {
    "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
  }]

but i want remove the whole data of that guy whose status is Archived. 但我想删除状态为已存档的那个家伙的全部数据。 So can any one tell me how can i do this. 所以任何人都可以告诉我我该怎么做。

One easy way to do this is to use array_filter . 一种简单的方法是使用array_filter

You'll need to first convert your json to an array: 您需要首先将json转换为数组:

$users = json_decode($json, true);

Now use filter the array of users, remove those with a status of Archived: 现在使用过滤用户数组,删除状态为已存档的用户:

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

Now $active will be an array of users that did not have a status of Archived. 现在, $active将是一系列状态为已存档的用户。

If you need the results back in json: 如果您需要将结果返回到json中:

$json = json_encode($active);

Here's a working example: https://3v4l.org/5KMaC 这是一个工作示例: https : //3v4l.org/5KMaC

you can use given code, it is working properly:- 您可以使用给定的代码,它可以正常工作:-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM