0

I am trying to filter out JSON response which I have decoded into an array. I can filter by a single objects value which in this case is the $lsr variable which is the commented out code below. What I am trying to do is now exclude all results which contain the value tx for the state. I am just getting null back or nothing at all. When I do a var_dump on $reportFeatures the array is empty and shouldn't be. What am I missing or over looking?

Here is a sample of a test JSON response I am working with. It should remove the first entry.

{"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"}}]}

Here is the code I am working with and been trying. For brevity I didn't post it all the way through the loop at the end.

$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'];
1
  • You have $reportType but check in_array on $eventType. Commented Sep 26, 2017 at 18:28

1 Answer 1

2

I suppose filter callback should be:

$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;
});
Sign up to request clarification or add additional context in comments.

2 Comments

Figured it had to be something with that $notIgnoredState line. Now what if I wanted to block everything but the state "tx"? Would I just change the ` $notIgnoredState` to ` $ignoredState? Probably rename the variable to something like $acceptedState`.
Yes, somehting like $acceptedState = $feature['place']['state'] == $yourState;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.