$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Loop Through PHP Array, Match Partial String, Return Values|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Loop Through PHP Array, Match Partial String, Return Values

matteradmin9PV0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 15 days ago.

Improve this question

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.

Post a comment

comment list (0)

  1. No comments so far