php - using in_array return next array if the in_array is empty -
i have array
$array = array( array('a', '18:27:15', 'label1'), array('b', '18:27:20', 'label2'), array('c', '18:27:25', 'label3'), array('d', '18:27:30', 'label4'), array('e', '18:27:35', 'label5'), array('f', '18:27:40', 'label6'), array('g', '18:27:45', 'label7'), array('h', '18:27:50', 'label8') );
im checking see whether have time string in array,for example if give
$end_time="18:27:25";
i array result , if give
$end_time="18:27:26";
i empty array. need return next array instead
array('d','18:27:30','label4')
below code
$arr_output = array(); foreach ($array $key=>$arr1){ if( in_array($end_time, $arr1) ){ $arr_output[$key]=$arr1; } } print_r(arr_output);//empty
you convert them timestamps , compare:
$result = null; $target = strtotime('18:27:41'); foreach ($array $item) { $compare = strtotime($item[1]); if ($compare >= $target) { if (null === $result) { $result = $item; } else if($target - strtotime($result[1]) < $target - $compare) { $result = $item; } } } echo print_r($result, 1);
with approach, :20
point 18:27:20
, , :21
point 18:27:25
.
note approach not need original array in particular order, each item considered.
Comments
Post a Comment