2017-09-26 5 views
0

Ich versuche, JSON-Antwort herauszufiltern, die ich in ein Array dekodiert habe. Ich kann nach einem einzelnen Objektwert filtern, in diesem Fall ist das die $ lsr-Variable, die der auskommentierte Code unten ist. Was ich versuche zu tun, schließt jetzt alle Ergebnisse aus, die den Wert tx für den Zustand enthalten. Ich bekomme nur null zurück oder gar nichts. Wenn ich eine var_dump auf $ reportFeatures mache, ist das Array leer und sollte es nicht sein. Was vermisse ich oder schaue ich über?PHP - JSON-Array aus mehreren Werten filtern

Hier ist ein Beispiel für eine JSON-Testantwort, mit der ich arbeite. Es sollte den ersten Eintrag entfernen.

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

Hier ist der Code, mit dem ich arbeite und habe versucht. Der Kürze halber habe ich es am Ende nicht durch die Schleife gepostet.

$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

Sie '$ reportType' haben, aber überprüfe' in_array' auf '$ eventType'. –

Antwort

2

Ich nehme Filter Rückruf sein sollte:

$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; 
}); 
+0

Es musste etwas mit dieser '$ notIgnoredState' Zeile sein. Was nun, wenn ich alles außer dem Staat "tx" blockieren wollte? Würde ich nur den '$ notIgnoredState' in' $ ignoredState' ändern? Benennen Sie die Variable wahrscheinlich in "$ acceptedState" um. –

+0

Ja, etwas wie $ acceptedState = $ feature ['place'] ['state'] == $ yourState; ' –

Verwandte Themen