0

我有一个如下的JSON

{"businessregistered":[
{"busiRegCount":"0","newdate":"2012-10-07"},{"busiRegCount":"0","newdate":"2012-10-08"},    
{"busiRegCount":"0","newdate":"2012-10-09"},{"busiRegCount":"0","newdate":"2012-10-10"},
{"busiRegCount":"3","newdate":"2012-10-11"},{"busiRegCount":"0","newdate":"2012-10-12"},
{"busiRegCount":"0","newdate":"2012-10-13"},{"busiRegCount":"0","newdate":"2012-10-14"},
{"busiRegCount":"0","newdate":"2012-10-15"},{"busiRegCount":"1","newdate":"2012-10-16"},
{"busiRegCount":"0","newdate":"2012-10-17"},{"busiRegCount":"0","newdate":"2012-10-18"},
{"busiRegCount":"0","newdate":"2012-10-19"},{"busiRegCount":"0","newdate":"2012-10-20"},
{"busiRegCount":"0","newdate":"2012-10-21"},{"busiRegCount":"0","newdate":"2012-10-22"}
],"businessactive":[],"resellerregistered":[],"reselleractive":[],"campaignregistered":
[],"campaignactive":[],"campaignexpiring":[],"revenuegenerated":[],"registrationpasscode":
[],"topuppasscode":[],"keywordmatched":[]}

需要的是

{"businessregistered":[
{"busiRegCount":"3","newdate":"2012-10-11"},{"busiRegCount":"0","newdate":"2012-10-12"},
{"busiRegCount":"0","newdate":"2012-10-13"},{"busiRegCount":"0","newdate":"2012-10-14"},
{"busiRegCount":"0","newdate":"2012-10-15"},{"busiRegCount":"1","newdate":"2012-10-16"},
{"busiRegCount":"0","newdate":"2012-10-17"},{"busiRegCount":"0","newdate":"2012-10-18"},
{"busiRegCount":"0","newdate":"2012-10-19"},{"busiRegCount":"0","newdate":"2012-10-20"},
{"busiRegCount":"0","newdate":"2012-10-21"},{"busiRegCount":"0","newdate":"2012-10-22"}
],"businessactive":[],"resellerregistered":[],"reselleractive":[],"campaignregistered":
[],"campaignactive":[],"campaignexpiring":[],"revenuegenerated":[],"registrationpasscode":
[],"topuppasscode":[],"keywordmatched":[]}

我需要从我busiRegCount > 0在 PHP 中获得的地方开始 JSON ...

帮忙

4

2 回答 2

2

您需要先解码 json,然后反复丢弃数据,然后将其返回到 json

$data = json_decode($json);

while(count($data->businessregistered) AND $data->businessregistered[0]->busiRegCount == 0) {
    array_shift($data->businessregistered);
}

$json = json_encode($data);

(假设 $json 包含 json 数据)

于 2013-04-22T04:49:24.967 回答
1
$array = json_decode($json, true);
$array['businessregistered'] = array_filter($array['businessregistered'], function ($i) {
    static $keep = false;
    return $keep = $keep || $i['busiRegCount'] > 0;
});
$json = json_encode($array);
于 2013-04-22T04:51:22.283 回答