I am parsing a JSON file and I have filtered out the values I wanted based on a certain key then loop through it to create variables for the other key values I want. Now that I have it sorted I am not sure how to order it so it displays in the order I want. Is there a simplified way to filter and order in one quick function or do I need to order it separately and if so what would be the best approach?
Here is a snippet from the code for filtering the array based on the "event" key before the loop. Also the order below is the order in which I want them displayed when output too.
$str = file_get_contents($url); // put the contents of the file into a variable
$o=json_decode($str,true);
$features=$o['features'];
// Lets filter the response to get only the values we want
$filtered = array_filter($features,function($el){
$alerts = array('Tornado Warning', 'Severe Thunderstorm Warning', 'Hurricane Warning', 'Tropical Storm Warning', 'Flash Flood Warning', 'Flood Warning', 'Tornado Watch', 'Severe Thunderstorm Watch', 'Hurricane Watch', 'Tropical Storm Watch', 'Flash Flood Watch');
return in_array($el['properties']['event'],$alerts);
});
$featuresand what is your expected output?$features) gives your question context and allows readers to verify for themselves if the accepted answer does, in fact, work as well as helps them to understand how the process works. Even though you have received your answer, your question should still be improved/edited to include this vital component of a good question. We don't know if there may be duplicate values in$featuresnor how it should be handled/sorted when there is a tie/duplicate $alert value inevent. SO pages help countless readers in the future (or at least they should).