我有两个数组。第一个是包含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-09 no data
2014-01-09 no data
2014-01-10 no data
2014-01-10 2014-01-10 13.37 13.37
2014-01-10 no data
2014-01-11 no data
2014-01-11 no data
2014-01-11 no data
2014-01-12 no data
2014-01-12 no data
2014-01-12 no data
2014-01-13 no data
2014-01-13 no data
2014-01-13 no data
2014-01-14 no data
2014-01-14 no data
2014-01-14 2014-01-14 12.88 12.88
2014-01-15 no data
2014-01-15 no data
2014-01-15 no data
我想收到的是这个。
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
请帮助:)
答案 0 :(得分:1)
you need to make your array with date key
foreach ($this->routes as $index=>$route) {
if ($route['dateendRoute'] == $date) {
$array[$date]=$your_value_is_here;
} else {
$array[$date]=$date." There is no data available ";
}
}
and then print your array respective with your view
答案 1 :(得分:1)
我按如下方式创建了一个较小的数据集:
$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>';
}
这产生了我认为你正在寻找的东西:
2014-01-09 no data
2014-01-10 2014-01-10 2 2
2014-01-11 no data
答案 2 :(得分:1)
模拟数组
$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>';
}
}
这将产生以下输出
2014-01-09 2014-01-09 1.1 1.1
2014-01-10 no data
2014-01-11 2014-01-11 2.2 2.2
2014-01-12 no data
2014-01-13 no data
2014-01-14 2014-01-14 3.3 3.3
2014-01-15 no data