Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Foreach循环中的PHP编辑值_Php - Fatal编程技术网

Foreach循环中的PHP编辑值

Foreach循环中的PHP编辑值,php,Php,试图从foreach循环中的值创建字符串,确实没有更好的方法,但它没有设置字符串。我不能将它重置为清空每个迭代,因为我需要它继续,我已经阅读了重复的问题,他们没有回答为什么它不起作用,感谢任何帮助 这个foreach只是循环检查具有多个小时的业务的小时格式 $hourspaited=['5:00am-11:00am','5:00pm-2:00am'] $hoursFinal = ''; foreach($hoursSpliced as $hour) { $

试图从foreach循环中的值创建字符串,确实没有更好的方法,但它没有设置字符串。我不能将它重置为清空每个迭代,因为我需要它继续,我已经阅读了重复的问题,他们没有回答为什么它不起作用,感谢任何帮助

这个foreach只是循环检查具有多个小时的业务的小时格式

$hourspaited=['5:00am-11:00am','5:00pm-2:00am']

      $hoursFinal = '';
      foreach($hoursSpliced as $hour) {
        $hours = explode(' - ', $hour, 2);
        //If the hours match the format, we continue onward, no need to touch those
        if(preg_match($regex, $hour)) {
          $hoursFinal = $hoursFinal . $hour;
          continue;
        }
        //0 stores opening hours, 1 stores closing hours
        //Lower them so we can compare
        $hours[0] = strtolower(trim($hours[0]));
        $hours[1] = strtolower(trim($hours[1]));
        //If it's in the AM we add it to the end, same goes for PM, if there isn't one we assume it's PM
        if(strpos($hours[0], 'am') !== false) {
          $hoursOpen = array_map('trim', explode('am', $hours[0]));
          $hoursOpen[1] = 'am';
        } elseif(strpos($hours[0], 'pm') !== false) {
          $hoursOpen = array_map('trim', explode('pm', $hours[0]));
          $hoursOpen[1] = 'pm';
        } else {
          $hoursOpen = [$hours[0]];
          $hoursOpen[] = 'pm';
        }
        //Same thing as above for closing hours
        if(strpos($hours[1], 'am') !== false) {
          $hoursClosed = array_map('trim', explode('am', $hours[1]));
          $hoursClosed[1] = 'am';
        } elseif(strpos($hours[1], 'pm') !== false) {
          $hoursClosed = array_map('trim', explode('pm', $hours[1]));
          $hoursClosed[1] = 'pm';
        } else {
          $hoursClosed = [$hours[1]];
          $hoursClosed[] = 'pm';
        }
        $hoursFinal .= ", $hoursOpen[0] $hoursOpen[1] - $hoursClosed[0] $hoursClosed[1]";
      }
      $hoursFinal = trim(', ', $hoursFinal);
      dd($hoursFinal);
明白了,花了我一段时间 你把修剪向后了

  $hoursFinal = trim(', ', $hoursFinal);
应该是

   $hoursFinal = trim( $hoursFinal, ", ");

如果不知道
$hourspaited
值,我怀疑是否有人能提供帮助。啊,对不起,威尔将其添加到主要问题中,我认为还有改进的余地。不必要的任务等。。。例如,
$hoursFinal=$hoursFinal$小时;继续是没有意义的。但是我需要一些数据来看看有什么更好的方法。。lolAnd什么是你的
$regex
我不记得一个空的regex算不算全部没有什么是尴尬的,只有当你不问而且它从来都不起作用的时候才是尴尬的