删除由 php unset 创建的数字键
Removing number key create by php unset
我尝试根据当前日期和时间过滤 json 数据:
我的json是:
{ "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 }
过滤 php 是:
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]); } }
在上面的代码中 json 被过滤,响应是
{"responseData":{"2":{"totalValue":0,"active":"Jun 25, 2019 6:34:59 PM","expired":"Oct 16, 2020 11:10:32 AM"}},"responseMessage":"success","responseCode":0}
我的问题是,如何在 json 响应中删除“2”?
我预期的 json 响应是:
{"responseData":{"totalValue":0,"active":"Jun 25, 2019 6:34:59 PM","expired":"Oct 16, 2020 11:10:32 AM"},"responseMessage":"success","responseCode":0}
这是我试过的完整脚本:
https://3v4l.org/SPhRi
谢谢
先像现在一样过滤,然后使用
$json['responseData'] = array_values($json['responseData']);
对数组重新编号。
我尝试根据当前日期和时间过滤 json 数据:
我的json是:
{ "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 }
过滤 php 是:
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]); } }
在上面的代码中 json 被过滤,响应是
{"responseData":{"2":{"totalValue":0,"active":"Jun 25, 2019 6:34:59 PM","expired":"Oct 16, 2020 11:10:32 AM"}},"responseMessage":"success","responseCode":0}
我的问题是,如何在 json 响应中删除“2”?
我预期的 json 响应是:
{"responseData":{"totalValue":0,"active":"Jun 25, 2019 6:34:59 PM","expired":"Oct 16, 2020 11:10:32 AM"},"responseMessage":"success","responseCode":0}
这是我试过的完整脚本: https://3v4l.org/SPhRi
谢谢
先像现在一样过滤,然后使用
$json['responseData'] = array_values($json['responseData']);
对数组重新编号。