我有一个包含单个数组(从db中提取)的数组,其内容与此类似:
return view('viewFileHere')
我想要做的是建立一个基于前一个数组的新的多维数组,其中具有相同月份的数组以下列方式连接在一起:
Array
(
[0] => Array
(
[date] => 2014-11
[time] => 2135
[name] => John
)
[1] => Array
(
[date] => 2014-11
[time] => 5496
[name] => Adam
)
[2] => Array
(
[date] => 2014-12
[time] => 1526
[name] => John
)
[3] => Array
(
[date] => 2014-12
[time] => 5481
[name] => Adam
)
[4] => Array
(
[date] => 2014-12
[time] => 3476
[name] => Lizzie
)
)
我试图查看各种数组函数,但根本无法解决如何实现这个问题....
答案 0 :(得分:1)
查看以下代码段。
$output = array(); // Where the output will be saved
foreach ($input as $row) { // Need to process the original input array
$date = $row['date']; // Grouping by the date value, thus we use it as an index in an associative array
if (empty($output[$date])) {
$output[$date] = array('date' => $date); // Make sure the 'date' value is in the final output
}
$output[$date][$row['name']] = $row['time']; // Actual values, e.g., [Adam] => 5496
}
$output = array_values($output); // Removing original indexes from the associative array
问题中所需的数组结构有点奇怪,但毫无疑问。