根据PHP中的值移动JSON

时间:2013-04-22 04:42:54

标签: php arrays json

我有一个 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":[]}

我需要从PHP中获取busiRegCount > 0的地方启动JSON ...

帮助

2 个答案:

答案 0 :(得分: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数据)

答案 1 :(得分: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);