Search code examples
phpjsonunset

Removing number key create by php unset


I tried to filtering json data based on current date and time:

my json is:

{ "responseData": [ { "totalValue": 0.0, "active": "Oct 16, 2019 11:16:23 AM", "expired": "Mar 28, 2020 3:15:58 PM" }, { "totalValue": 0.0, "active": "Oct 16, 2019 11:16:23 AM", "expired": "Mar 28, 2020 3:15:58 PM" }, { "totalValue": 0, "active": "Jun 25, 2019 6:34:59 PM", "expired": "Oct 16, 2020 11:10:32 AM", } ], "responseMessage": "success", "responseCode": 0 }

and filtering over php is:

date_default_timezone_set('UTC'); json_decode($mybalance, true); foreach($json['responseData'] as $key=>$value){ if(strtotime($value['expired']) < strtotime(date('M d, Y H:i:s A'))){ unset($json['responseData'][$key]); } }

within above code the json filtered, and the response is

{"responseData":{"2":{"totalValue":0,"active":"Jun 25, 2019 6:34:59 PM","expired":"Oct 16, 2020 11:10:32 AM"}},"responseMessage":"success","responseCode":0}

my question is, how to remove "2" at the json response?

my expected json response is:

{"responseData":{"totalValue":0,"active":"Jun 25, 2019 6:34:59 PM","expired":"Oct 16, 2020 11:10:32 AM"},"responseMessage":"success","responseCode":0}

Here the complete script i tried: https://3v4l.org/SPhRi

thanks


Solution

  • First filter like you do now, then use

    $json['responseData'] = array_values($json['responseData']);
    

    to renumber the array.