Search through all levels of a PHP array -
apologies if duplicate question, i'm not familiar exact terminology i'm trying achieve i've been unsuccessful in searching answer far. i'm pretty green stuff, , following has come through trial , error - not because i'm under illusions it's right.
here's i'm trying achieve:
- using textarea, clients enter custom opening times (one per line): day (name), date, status (open or closed), opening time, closing time. example: 'christmas day,2016-12-25,closed' or 'boxing day,2016-12-26,open,8:00,17:00'.
- this textarea content split array new line, , split again comma.
- using these arrays, search whether date today's date and, if is, show more content arrays (like opening time particular day), or - if there's no match - display regular opening hours (already set elsewhere).
what i've got far (note i've retrieved $custom_dates variable content):
$array = array(); $array = preg_split('/\r/', $custom_dates); /* split line break */ foreach($array $key => $custom_dates) { $array[$key] = explode(',', $custom_dates); /* split again comma */ };
this returns following array (which longer or shorter depending on client's needs):
array ( [0] => array ( [0] => christmas day [1] => 2016-12-25 [2] => closed ) [1] => array ( [0] => boxing day [1] => 2016-12-26 [2] => open [3] => 8:00 [4] => 17:00 ) )
what i'm struggling how search specific date match in of arrays. if use if($array[$key][1] == $date) (where $date set today's date based on timezone) searches last array, in case boxing day information. if $date 2016-12-26, everything's great , go ahead , show boxing day opening times.
but if today's $date 2016-12-25 - though there's information specific date - nothing happens because $key in if statement checking boxing day array information. means continuing check rest (whether open or closed, etc.) redundant because it's not getting date right.
my (long-winded) question is: can use instead of $array[$key][1] in order search of array levels find match last [1]? or have come @ wrong angle?
please me out if can, i'm pulling hair out because don't know right terms search for. happy provide more details if need any.
thank in advance.
you accomplish inside foreach loop you've created.
$array = preg_split('/\r/', $custom_dates); /* split line break */ foreach ($array $key => $custom_date_string) { $custom_date = explode(',', $custom_date_string); if ($custom_date[1] === $today) { // stuff } $array[$key] = $custom_date; // if still need }
Comments
Post a Comment