我正在尝试过滤掉已解码为数组的JSON响应。我可以通过单个对象值进行过滤,在这种情况下是$ lsr变量,这是下面注释掉的代码。我想要做的是现在排除包含状态值tx的所有结果。我只是无效或者什么都没有。当我在$ reportFeatures上执行var_dump时,数组是空的,不应该是。我错过了什么或者看不到什么?
以下是我正在使用的测试JSON响应的示例。它应该删除第一个条目。
{"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'];
答案 0 :(得分:2)
我认为过滤回调应该是:
$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;
});