PHP-从多个值筛选JSON数组
我试图过滤掉已解码到数组中的JSON响应。我可以通过单个objects值进行过滤,在本例中,该值是$lsr变量,它是下面注释掉的代码。我现在要做的是排除包含状态值tx的所有结果。我只是得到零回或什么都没有。当我在$reportFeatures上执行var_转储时,数组是空的,不应该是空的。我错过了什么或是错过了什么 下面是我正在处理的一个测试JSON响应的示例。它应该删除第一个条目PHP-从多个值筛选JSON数组,php,arrays,json,Php,Arrays,Json,我试图过滤掉已解码到数组中的JSON响应。我可以通过单个objects值进行过滤,在本例中,该值是$lsr变量,它是下面注释掉的代码。我现在要做的是排除包含状态值tx的所有结果。我只是得到零回或什么都没有。当我在$reportFeatures上执行var_转储时,数组是空的,不应该是空的。我错过了什么或是错过了什么 下面是我正在处理的一个测试JSON响应的示例。它应该删除第一个条目 {"success":true,"error":null,"response":[{"id":"59c84e0bd
{"success":true,"error":null,"response":[{"id":"59c84e0bdb6be875458b45fd","loc":{"long":-100.73,"lat":38.47},"report":{"code":"G","type":"thunderstorm wind gust","name":"1 mi SSW Grigston","detail":{"text":60,"windSpeedKTS":52,"windSpeedKPH":97,"windSpeedMPH":60},"reporter":"public","comments":"Dime size hail also occurred at this location.","timestamp":1506296700,"cat":"wind","dateTimeISO":"2017-09-24T18:45:00-05:00","datetime":"2017-09-24T18:45:00-05:00","wfo":"ddc"},"place":{"name":"grigston","state":"tx","county":"scott","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c84703db6be8151f8b45fd","loc":{"long":-100.79,"lat":38.37},"report":{"code":"G","type":"thunderstorm wind gust","name":"7 mi E Shallow Water","detail":{"text":68,"windSpeedKTS":59,"windSpeedKPH":109,"windSpeedMPH":68},"reporter":"public","comments":"Measured 68 mph with a home anemometer.","timestamp":1506296640,"cat":"wind","dateTimeISO":"2017-09-24T18:44:00-05:00","datetime":"2017-09-24T18:44:00-05:00","wfo":"ddc"},"place":{"name":"shallow water","state":"ks","county":"scott","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c84703db6be8151f8b45fe","loc":{"long":-100.79,"lat":38.37},"report":{"code":"H","type":"hail","name":"7 mi E Shallow Water","detail":{"text":1,"hailIN":1,"hailMM":25.4},"reporter":"public","comments":"Reported most marble with some quarter sized hail.","timestamp":1506296640,"cat":"hail","dateTimeISO":"2017-09-24T18:44:00-05:00","datetime":"2017-09-24T18:44:00-05:00","wfo":"ddc"},"place":{"name":"shallow water","state":"ks","county":"scott","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c848a8db6be82e288b4631","loc":{"long":-100.75,"lat":38.4},"report":{"code":"H","type":"hail","name":"6 mi SSW Grigston","detail":{"text":0.75,"hailIN":0.75,"hailMM":19.05},"reporter":"trained spotter","comments":"","timestamp":1506296400,"cat":"hail","dateTimeISO":"2017-09-24T18:40:00-05:00","datetime":"2017-09-24T18:40:00-05:00","wfo":"ddc"},"place":{"name":"grigston","state":"ks","county":"scott","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c83ffadb6be81b788b4602","loc":{"long":-94.13,"lat":46.33},"report":{"code":"D","type":"thunderstorm wind damage","name":"3 mi ESE Brainerd","detail":{"text":0},"reporter":"trained spotter","comments":"Numerous 6-8\" tree branches down along with a few stripped pine trees. time estimated from radar.","timestamp":1506288480,"cat":"wind","dateTimeISO":"2017-09-24T16:28:00-05:00","datetime":"2017-09-24T16:28:00-05:00","wfo":"dlh"},"place":{"name":"brainerd","state":"mn","county":"crow wing","country":"us"},"profile":{"tz":"America\/Chicago"}}]}
这是我正在使用并一直在尝试的代码。为了简洁起见,我并没有在最后的循环中一直发布它
$json = file_get_contents($url); // put the contents of the file into a variable
$result = json_decode($json, true);
$features=$result['response'];
// Lets filter the response to get only the values we want
$lsr = array(
'hurricane',
'tropical storm',
'storm surge',
'water spout',
'tornado',
'funnel cloud',
'wall cloud',
'thunderstorm wind damage',
'thunderstorm wind gust',
'hail',
'lightning',
'flash flood',
'flood',
'blizzard',
'heavy snow',
'snow',
'sleet',
'freezing rain',
);
/*
// filter features, remove those which are not of any of the desired event types
$reportFeatures = array_filter($features, function(array $feature) use ($lsr) {
$reportType = $feature['report']['type'];
return in_array($reportType, $lsr);
});
*/
// Lets filter the response to get the values we dont want
$ignoreState = 'tx';
// filter features, remove those which are not of any of the desired event types
// and also remove those from $ignoreState
$reportFeatures = array_filter($features, function (array $feature) use ($lsr, $ignoreState) {
$reportType = $feature['report']['type'];
$state = $feature['place']['state'];
$isValidEvent = in_array($eventType, $lsr);
return $ignoreState && $isValidEvent;
});
//var_dump($reportFeatures);
foreach($reportFeatures as $report) {
$reportID = $report['id'];
$type = $report['report']['type'];
$city = $report['report']['name'];
$county = $report['place']['county'];
$County = ucwords($county);
$state = $report['place']['state'];
$State = strtoupper($state);
$mag = $report['report']['detail']['text'];
$reporter = $report['report']['reporter'];
$Reporter = ucwords($reporter);
$comments = $report['report']['comments'];
我认为过滤器回调应该是:
$reportFeatures = array_filter($features, function (array $feature) use ($lsr, $ignoreState) {
// check if event is valid
$isValidEvent = in_array($feature['report']['type'], $lsr);
// check if state is not equal to `$ignoreState`
$notIgnoredState = $feature['place']['state'] != $ignoreState;
return $notIgnoredState && $isValidEvent;
});
我认为过滤器回调应该是:
$reportFeatures = array_filter($features, function (array $feature) use ($lsr, $ignoreState) {
// check if event is valid
$isValidEvent = in_array($feature['report']['type'], $lsr);
// check if state is not equal to `$ignoreState`
$notIgnoredState = $feature['place']['state'] != $ignoreState;
return $notIgnoredState && $isValidEvent;
});
您有
$reportType
但在$eventType
上的\u数组中检查。您有$reportType
但在$eventType
上检查但在\u数组中检查。我想它一定是有$notIgnoredState
行的。现在,如果我想阻止除“tx”状态之外的所有内容,该怎么办?我是否可以将“$notIgnoredState”更改为“$ignoredState?可能将变量重命名为类似于
$acceptedState`。是的,类似于$acceptedState=$feature['place']['state']=$yourState
认为它必须与$notIgnoredState
行有关。现在,如果我想阻止除“tx”状态之外的所有内容,该怎么办?我是否可以将“$notIgnoredState”更改为“$ignoredState?可能将变量重命名为类似于
$acceptedState`。是的,类似于$acceptedState=$feature['place']['state']=$yourState代码>