我有来自数据库的结果集
Array
(
[0] => Array
(
[StartTime] => 2014-06-24 12:30:00
[EndTime] => 2014-06-24 13:50:00
[Date] => 2014-06-24
[BookingId] => 2
[TableId] => 39
)
[1] => Array
(
[StartTime] => 2014-06-24 13:50:00
[EndTime] => 2014-06-24 16:20:00
[Date] => 2014-06-24
[BookingId] => 3
[TableId] => 39
)
[2] => Array
(
[StartTime] => 2014-06-24 06:30:00
[EndTime] => 2014-06-24 07:10:00
[Date] => 2014-06-24
[BookingId] => 4
[TableId] => 37
)
[7] => Array
(
[StartTime] => 2014-06-29 06:30:00
[EndTime] => 2014-06-29 07:00:00
[Date] => 2014-06-29
[BookingId] => 9
[TableId] => 39
)
[9] => Array
(
[StartTime] => 2014-06-26 12:30:00
[EndTime] => 2014-06-26 15:00:00
[Date] => 2014-06-26
[BookingId] => 11
[TableId] => 37
)
)
我需要数组如下所示请帮帮我,
Array
(
[0] => Array
(
[Date] => 2014-06-24
[Tables] => Array
(
[0] => Array
(
[TableId] => 39
[Reservations] => Array
(
[0] => Array
(
[BookingId] => 2
)
[1] => Array
(
[[BookingId] => 3
)
)
)
[1] => Array
(
[TableId] => 37
[Reservations] => Array
(
[0] => Array
(
[BookingId] => 4
)
)
)
)
)
[1] => Array
(
[Date] => 2014-06-26
[Tables] => Array
(
[0] => Array
(
[TableId] => 37
[Reservations] => Array
(
[0] => Array
(
[BookingId] => 11
)
)
)
)
)
[2] => Array
(
[Date] => 2014-06-29
[Tables] => Array
(
[0] => Array
(
[TableId] => 39
[Reservations] => Array
(
[0] => Array
(
[BookingId] => 9
)
)
)
)
)
)
我已使用以下代码在CODEIGNITER Framework中获取结果php,
我试过这样,
function example(){
$oldArray = $this->db->query("Select StartTime,EndTime,date(StartTime) as Date,BookingId,TableId from tblbooking")->result_array();
foreach ($oldArray as $row){
$data = $row['Date'];
$newArray[$row['Date']][$row['TableId']][] = array('TableId' => $row['TableId'],'Date' => $row['Date'],'BookingId' => $row['BookingId']);
}
foreach($newArray as $index => $back){
foreach($back as $index2 => $back2){
$t3[] = array( 'Date' => $index,'Tables' => $back);
}
}
print_r($t3);
}
但是我无法获得上面的数组列表 所以请帮帮我
答案 0 :(得分:0)
未经测试,但有类似的事情。
首先,将数组排序为日期顺序,并将日期中的tableid顺序排序(您可以在获取数组数据的SQL查询中执行此操作)。
然后循环跟踪日期/表格的变化,设置日期/表格的计数器。
<?php
$Out = array();
$prev_date = '';
$prev_table = '';
$day = -1;
$table = -1;
// Sort the array into date order, and table order within date
usort($some_array, "cmp");
foreach($some_array AS $some_array_item)
{
if ($some_array_item['Date'] != $prev_date)
{
$day++;
$Out[$day] = array();
$Out[$day]['Date'] = $some_array_item['Date'];
$Out[$day]['Tables'] = array();
$table = -1;
$prev_date = $some_array_item['Date'];
$prev_table = '';
}
if ($some_array_item['TableId'] != $prev_table)
{
$table++;
$Out[$day]['Tables'][$table]['TableId'] = $some_array_item['TableId'];
$Out[$day]['Tables'][$table]['Reservations'] = array();
$prev_table = $some_array_item['TableId'];
}
$Out[$day]['Tables'][$table]['Reservations'][] = array('BookingId'=>$some_array_item['BookingId']);
}
function cmp($a, $b)
{
if ($a['Date'] == $b['Date'])
{
if ($a['Date']['TableId'] == $b['Date']['TableId'])
{
return 0;
}
return ($a['Date']['TableId'] < $b['Date']['TableId']) ? -1 : 1;
}
return ($a['Date'] < $b['Date']) ? -1 : 1;
}
?>