I am working with Wordpress to build a weather website and I have a little bit of a complicated piece I'm trying to do. I have a static JSON array of record high temperatures for each date in a year. What I'm trying to do is loop through the array and find the numerical month and day that matches the current date and return the year and the temperature. So for example, today is May 8 (05-08), so I want to find 05-08 in the array and return the year and temperature that its in that object in the array. So in the case of the sample I pulled below the returns would by 1986 for the year and 92 for the temperature. These returns will be going into ACF fields (which I already know how to do once I get the values)
Sample of the array as JSON:
$json = '{"smry":[["89","1955-05-05"],["94","1952-05-06"],["96","1952-05-07"],
["92","1986-05-08"],["92","1962-05-09"],["92","1955-05-10"],["90","1963-05-11"],
["92","1962-05-12"],["90","2018-05-13"]]}';
I, of course, have it decoding into a PHP Array:
$json_data = json_decode($json, true);
From here is where I'm kind of stuck - below you will find very incorrect code, but its just to show more of what I'm trying to get at. I know I likely need to use a foreach and an if statement, but how to construct it is where I'm a bit stuck.
$currentDate = date('m-d');
foreach ($json_data["smry"] as [$temp, $date]) {
if ($date contains $currentDate) { /* I think this is where I'm stuck mostly */
$recordYr = substr($date, 0, 4);
$recordTemp = $temp;
}
}
Any help would be greatly appreciated.
str_ends_withfor the test. And I recommend to test with-05-08otherwise you can have surprise like catching2005-08.