我想筛选出我解码成数组的JSON响应。我可以过滤一个单一的对象值,在这种情况下,它是$ lsr变量,它是下面注释掉的代码。我现在想要做的是排除包含状态值tx的所有结果。我只是退回或什么也没有。当我在$ reportFeatures上做一个var_dump时,数组是空的,不应该是。我在想什么?PHP - 从多个值筛选JSON数组
以下是我正在使用的测试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'];
你'$ reportType'但检查'$'eventType' in_array'。 –