Php 如何循环两个数组,如果值相等,则返回某个值,而不是其他值

Php 如何循环两个数组,如果值相等,则返回某个值,而不是其他值,php,loops,Php,Loops,我有两个数组。第一个是一个包含290个日期的数组。第二个是带有3个日期的数组。 我尝试了很多事情,但没有一件成功。 这是我的密码 foreach ($this->routes as $index=>$route) { if ($route['dateendRoute'] == $date) { echo '<tr>'; echo '<td>' . $date . '</td>'; echo '<td>' . $

我有两个数组。第一个是一个包含290个日期的数组。第二个是带有3个日期的数组。 我尝试了很多事情,但没有一件成功。 这是我的密码

foreach ($this->routes as $index=>$route) {
 if ($route['dateendRoute'] == $date) {
    echo '<tr>';
    echo '<td>' . $date . '</td>';
    echo '<td>' . $route['dateendRoute'] . '</td>';
    echo '<td>' . $route['total_distance'] . '</td>';
    echo '<td>' . $route['total_distance'] . '</td>';
    echo '<tr>';
    unset($this->routes[$index]);
 } else {
    echo '<tr>';
    echo '<td>' . $date . '</td>';
    echo '<td colspan=3>no data</td>';
    echo '</tr>';
 }
}
我想要的是这个

2014-01-09  2014-01-09  12.01   12.01
2014-01-10  2014-01-10  13.37   13.37
2014-01-11  no data
2014-01-12  no data
2014-01-13  no data
2014-01-14  2014-01-14  12.88   12.88
2014-01-15  no data

请帮助:)

我创建了一个较小的数据集,如下所示:

$dates = array(
    '2014-01-09',
    '2014-01-10',
    '2014-01-11',
);

$routes = array(
    array(
        'dateendRoute'=>'2014-01-10',
        'total_distance'=>'2',
    ),
);
foreach ($this->dates as $date) {
    $routeIndex = -1;

    for ($i = 0; $i < count($this->routes); $i++) {
        if ($this->routes[$i]['dateendRoute'] == $date) {
            $routeIndex = $i;
            break;
        }
    }

    echo '<tr>';
    echo '<td>' . $date . '</td>';
    if ($routeIndex != -1) {
        echo '<td>' . $this->routes[$routeIndex]['dateendRoute'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
    } else {
        echo '<td colspan=3>no data</td>';
    }
    echo '</tr>';
}
然后我更新了你的函数如下:

$dates = array(
    '2014-01-09',
    '2014-01-10',
    '2014-01-11',
);

$routes = array(
    array(
        'dateendRoute'=>'2014-01-10',
        'total_distance'=>'2',
    ),
);
foreach ($this->dates as $date) {
    $routeIndex = -1;

    for ($i = 0; $i < count($this->routes); $i++) {
        if ($this->routes[$i]['dateendRoute'] == $date) {
            $routeIndex = $i;
            break;
        }
    }

    echo '<tr>';
    echo '<td>' . $date . '</td>';
    if ($routeIndex != -1) {
        echo '<td>' . $this->routes[$routeIndex]['dateendRoute'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
    } else {
        echo '<td colspan=3>no data</td>';
    }
    echo '</tr>';
}

我创建了一个较小的数据集,如下所示:

$dates = array(
    '2014-01-09',
    '2014-01-10',
    '2014-01-11',
);

$routes = array(
    array(
        'dateendRoute'=>'2014-01-10',
        'total_distance'=>'2',
    ),
);
foreach ($this->dates as $date) {
    $routeIndex = -1;

    for ($i = 0; $i < count($this->routes); $i++) {
        if ($this->routes[$i]['dateendRoute'] == $date) {
            $routeIndex = $i;
            break;
        }
    }

    echo '<tr>';
    echo '<td>' . $date . '</td>';
    if ($routeIndex != -1) {
        echo '<td>' . $this->routes[$routeIndex]['dateendRoute'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
    } else {
        echo '<td colspan=3>no data</td>';
    }
    echo '</tr>';
}
然后我更新了你的函数如下:

$dates = array(
    '2014-01-09',
    '2014-01-10',
    '2014-01-11',
);

$routes = array(
    array(
        'dateendRoute'=>'2014-01-10',
        'total_distance'=>'2',
    ),
);
foreach ($this->dates as $date) {
    $routeIndex = -1;

    for ($i = 0; $i < count($this->routes); $i++) {
        if ($this->routes[$i]['dateendRoute'] == $date) {
            $routeIndex = $i;
            break;
        }
    }

    echo '<tr>';
    echo '<td>' . $date . '</td>';
    if ($routeIndex != -1) {
        echo '<td>' . $this->routes[$routeIndex]['dateendRoute'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
    } else {
        echo '<td colspan=3>no data</td>';
    }
    echo '</tr>';
}

我创建了一个较小的数据集,如下所示:

$dates = array(
    '2014-01-09',
    '2014-01-10',
    '2014-01-11',
);

$routes = array(
    array(
        'dateendRoute'=>'2014-01-10',
        'total_distance'=>'2',
    ),
);
foreach ($this->dates as $date) {
    $routeIndex = -1;

    for ($i = 0; $i < count($this->routes); $i++) {
        if ($this->routes[$i]['dateendRoute'] == $date) {
            $routeIndex = $i;
            break;
        }
    }

    echo '<tr>';
    echo '<td>' . $date . '</td>';
    if ($routeIndex != -1) {
        echo '<td>' . $this->routes[$routeIndex]['dateendRoute'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
    } else {
        echo '<td colspan=3>no data</td>';
    }
    echo '</tr>';
}
然后我更新了你的函数如下:

$dates = array(
    '2014-01-09',
    '2014-01-10',
    '2014-01-11',
);

$routes = array(
    array(
        'dateendRoute'=>'2014-01-10',
        'total_distance'=>'2',
    ),
);
foreach ($this->dates as $date) {
    $routeIndex = -1;

    for ($i = 0; $i < count($this->routes); $i++) {
        if ($this->routes[$i]['dateendRoute'] == $date) {
            $routeIndex = $i;
            break;
        }
    }

    echo '<tr>';
    echo '<td>' . $date . '</td>';
    if ($routeIndex != -1) {
        echo '<td>' . $this->routes[$routeIndex]['dateendRoute'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
    } else {
        echo '<td colspan=3>no data</td>';
    }
    echo '</tr>';
}

我创建了一个较小的数据集,如下所示:

$dates = array(
    '2014-01-09',
    '2014-01-10',
    '2014-01-11',
);

$routes = array(
    array(
        'dateendRoute'=>'2014-01-10',
        'total_distance'=>'2',
    ),
);
foreach ($this->dates as $date) {
    $routeIndex = -1;

    for ($i = 0; $i < count($this->routes); $i++) {
        if ($this->routes[$i]['dateendRoute'] == $date) {
            $routeIndex = $i;
            break;
        }
    }

    echo '<tr>';
    echo '<td>' . $date . '</td>';
    if ($routeIndex != -1) {
        echo '<td>' . $this->routes[$routeIndex]['dateendRoute'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
    } else {
        echo '<td colspan=3>no data</td>';
    }
    echo '</tr>';
}
然后我更新了你的函数如下:

$dates = array(
    '2014-01-09',
    '2014-01-10',
    '2014-01-11',
);

$routes = array(
    array(
        'dateendRoute'=>'2014-01-10',
        'total_distance'=>'2',
    ),
);
foreach ($this->dates as $date) {
    $routeIndex = -1;

    for ($i = 0; $i < count($this->routes); $i++) {
        if ($this->routes[$i]['dateendRoute'] == $date) {
            $routeIndex = $i;
            break;
        }
    }

    echo '<tr>';
    echo '<td>' . $date . '</td>';
    if ($routeIndex != -1) {
        echo '<td>' . $this->routes[$routeIndex]['dateendRoute'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
        echo '<td>' . $this->routes[$routeIndex]['total_distance'] . '</td>';
    } else {
        echo '<td colspan=3>no data</td>';
    }
    echo '</tr>';
}
模拟阵列

$dates = array('2014-01-09','2014-01-10','2014-01-11','2014-01-12','2014-01-13','2014-01-14','2014-01-15');
$routes = array(
  array('dateendRoute' => '2014-01-09', 'total_distance' => '1.1'),
  array('dateendRoute' => '2014-01-11', 'total_distance' => '2.2'),
  array('dateendRoute' => '2014-01-14', 'total_distance' => '3.3')
);
循环浏览每个日期

foreach($dates as $date) {
  //assume no matches found
  $no_match = TRUE;
  foreach ($routes as $index=>$route) {
    if ($route['dateendRoute'] == $date) {
      //match found so switch this variable
      $no_match = FALSE;
      echo '<tr>';
      echo '<td>' . $date . '</td>';
      echo '<td>' . $route['dateendRoute'] . '</td>';
      echo '<td>' . $route['total_distance'] . '</td>';
      echo '<td>' . $route['total_distance'] . '</td>';
      echo '</tr>';
    }
  }
  //check if match found after looping through routes array
  if($no_match) {
    echo '<tr>';
    echo '<td>' . $date . '</td>';
    echo '<td colspan=3>no data</td>';
    echo '</tr>';
  }
}
模拟阵列

$dates = array('2014-01-09','2014-01-10','2014-01-11','2014-01-12','2014-01-13','2014-01-14','2014-01-15');
$routes = array(
  array('dateendRoute' => '2014-01-09', 'total_distance' => '1.1'),
  array('dateendRoute' => '2014-01-11', 'total_distance' => '2.2'),
  array('dateendRoute' => '2014-01-14', 'total_distance' => '3.3')
);
循环浏览每个日期

foreach($dates as $date) {
  //assume no matches found
  $no_match = TRUE;
  foreach ($routes as $index=>$route) {
    if ($route['dateendRoute'] == $date) {
      //match found so switch this variable
      $no_match = FALSE;
      echo '<tr>';
      echo '<td>' . $date . '</td>';
      echo '<td>' . $route['dateendRoute'] . '</td>';
      echo '<td>' . $route['total_distance'] . '</td>';
      echo '<td>' . $route['total_distance'] . '</td>';
      echo '</tr>';
    }
  }
  //check if match found after looping through routes array
  if($no_match) {
    echo '<tr>';
    echo '<td>' . $date . '</td>';
    echo '<td colspan=3>no data</td>';
    echo '</tr>';
  }
}
模拟阵列

$dates = array('2014-01-09','2014-01-10','2014-01-11','2014-01-12','2014-01-13','2014-01-14','2014-01-15');
$routes = array(
  array('dateendRoute' => '2014-01-09', 'total_distance' => '1.1'),
  array('dateendRoute' => '2014-01-11', 'total_distance' => '2.2'),
  array('dateendRoute' => '2014-01-14', 'total_distance' => '3.3')
);
循环浏览每个日期

foreach($dates as $date) {
  //assume no matches found
  $no_match = TRUE;
  foreach ($routes as $index=>$route) {
    if ($route['dateendRoute'] == $date) {
      //match found so switch this variable
      $no_match = FALSE;
      echo '<tr>';
      echo '<td>' . $date . '</td>';
      echo '<td>' . $route['dateendRoute'] . '</td>';
      echo '<td>' . $route['total_distance'] . '</td>';
      echo '<td>' . $route['total_distance'] . '</td>';
      echo '</tr>';
    }
  }
  //check if match found after looping through routes array
  if($no_match) {
    echo '<tr>';
    echo '<td>' . $date . '</td>';
    echo '<td colspan=3>no data</td>';
    echo '</tr>';
  }
}
模拟阵列

$dates = array('2014-01-09','2014-01-10','2014-01-11','2014-01-12','2014-01-13','2014-01-14','2014-01-15');
$routes = array(
  array('dateendRoute' => '2014-01-09', 'total_distance' => '1.1'),
  array('dateendRoute' => '2014-01-11', 'total_distance' => '2.2'),
  array('dateendRoute' => '2014-01-14', 'total_distance' => '3.3')
);
循环浏览每个日期

foreach($dates as $date) {
  //assume no matches found
  $no_match = TRUE;
  foreach ($routes as $index=>$route) {
    if ($route['dateendRoute'] == $date) {
      //match found so switch this variable
      $no_match = FALSE;
      echo '<tr>';
      echo '<td>' . $date . '</td>';
      echo '<td>' . $route['dateendRoute'] . '</td>';
      echo '<td>' . $route['total_distance'] . '</td>';
      echo '<td>' . $route['total_distance'] . '</td>';
      echo '</tr>';
    }
  }
  //check if match found after looping through routes array
  if($no_match) {
    echo '<tr>';
    echo '<td>' . $date . '</td>';
    echo '<td colspan=3>no data</td>';
    echo '</tr>';
  }
}

您需要按日期对数组进行排序,以便按时间顺序进行比较。我们可以看看其余的代码吗?循环日期的代码吗?您需要按日期对数组进行排序,以便按时间顺序进行比较。我们可以看看其余的代码吗,循环日期的代码?您需要按日期对数组进行排序,以便按时间顺序进行比较。我们可以看到其余的代码,即循环日期的代码吗?您需要按日期对数组进行排序,以便按时间顺序进行比较。我们可以看到其余的代码,即循环日期的代码吗?