我必须得到一个普通数组,按时间间隔分组创建一个多维数组。
原始数组是这样的:
$one = {
0 => [
'nome' => 'ALO/MANO'
'origem' => 'GRU'
'voo' => '4730'
'inicio' => '2018/03/10'
'hora' => '18:15'
],
1 => [
'nome' => 'JOHN/DOE'
'origem' => 'GRU'
'voo' => '4730'
'inicio' => '2018/03/10'
'hora' => '18:15'
],
2 => [
'nome' => 'EVA/DOE'
'origem' => 'GRU'
'voo' => '4730'
'inicio' => '2018/03/10'
'hora' => '18:15'
],
3 => [
'nome' => 'CARL/DOE'
'origem' => 'GRU'
'voo' => '4730'
'inicio' => '2018/03/10'
'hora' => '20:35'
],
4 => [
'nome' => 'CARL/DOE'
'origem' => 'GRU'
'voo' => '4730'
'inicio' => '2018/03/10'
'hora' => '20:55'
],
5 => [
'nome' => 'CARL/DOE'
'origem' => 'GRU'
'voo' => '4730'
'inicio' => '2018/03/10'
'hora' => '22:35'
],
46 => [
'nome' => 'CARL/DOE'
'origem' => 'GRU'
'voo' => '4730'
'inicio' => '2018/03/10'
'hora' => '22:55'
],
};
我必须通过创建一个按hora键分组的函数来实现这一点:$ result = groupByTime($ one,50);
$result = {
0 => [
0 => [
'nome' => 'ALO/RAFAEL'
'origem' => 'GRU'
'voo' => '4730'
'inicio' => '2018/03/10'
'hora' => '18:15'
],
1 => [
'nome' => 'JOHN/DOE'
'origem' => 'GRU'
'voo' => '4730'
'inicio' => '2018/03/10'
'hora' => '18:15'
],
2 => [
'nome' => 'EVA/DOE'
'origem' => 'GRU'
'voo' => '4730'
'inicio' => '2018/03/10'
'hora' => '18:15'
]
],
1 => [
0 => [
'nome' => 'CARL/DOE'
'origem' => 'GRU'
'voo' => '4730'
'inicio' => '2018/03/10'
'hora' => '20:35'
],
1 => [
'nome' => 'CARL/DOE'
'origem' => 'GRU'
'voo' => '4730'
'inicio' => '2018/03/10'
'hora' => '20:55'
],
2 => [
'nome' => 'CARL/DOE'
'origem' => 'GRU'
'voo' => '4730'
'inicio' => '2018/03/10'
'hora' => '22:35'
]
],
0 => [
0 => [
'nome' => 'CARL/DOE'
'origem' => 'GRU'
'voo' => '4730'
'inicio' => '2018/03/10'
'hora' => '22:55'
]
],
};
我已经尝试了几天,但似乎我被卡住了!
答案 0 :(得分:1)
我认为这就是你要找的东西
/**
* Group Elements Of One Array By Hora
* @param array $arr
* @return array
*/
function groupByTimeInterval( $arr )
{
$indexs = [];
$new_arr = [];
foreach ( $arr as $value ) {
!isset( $indexs[$value['hora']] ) && $indexs[$value['hora']] = count($new_arr);
$new_arr[$indexs[$value['hora']]][] = $value;
}
return $new_arr;
}
我使用给定的数组测试了它,我得到了以下结果:
[0] => Array
(
[0] => Array
(
[nome] => ALO/MANO
[origem] => GRU
[voo] => 4730
[inicio] => 2018/03/10
[hora] => 18:15
)
[1] => Array
(
[nome] => JOHN/DOE
[origem] => GRU
[voo] => 4730
[inicio] => 2018/03/10
[hora] => 18:15
)
[2] => Array
(
[nome] => EVA/DOE
[origem] => GRU
[voo] => 4730
[inicio] => 2018/03/10
[hora] => 18:15
)
)
[1] => Array
(
[0] => Array
(
[nome] => CARL/DOE
[origem] => GRU
[voo] => 4730
[inicio] => 2018/03/10
[hora] => 20:35
)
)
[2] => Array
(
[0] => Array
(
[nome] => CARL/DOE
[origem] => GRU
[voo] => 4730
[inicio] => 2018/03/10
[hora] => 20:55
)
...
修改强>
/**
* Group Elements Of One Array By Hora
* @param array $arr
* @param integer $inter
* @return array
*/
function groupByTimeInterval( $arr, $inter = 50 )
{
$indexs = [];
$new_arr = [];
foreach ( $arr as $row ) {
$new_arr[getIndex($indexs,$row,$inter)][] = $row;
}
return $new_arr;
}
/**
*
*/
function getIndex( &$indexs, $row, $inter )
{
//print_r( $indexs );
$date_time = $row['inicio'] . ' ' . $row['hora'];
foreach ($indexs as $index => $_date_times ){
foreach ($_date_times as $_date_time) {
if( $date_time == $_date_time ) return $index;
if( horaComp( $date_time, $_date_time, $inter ) ){
$indexs[$index][] = $date_time;
return $index;
}
}
}
return ( array_push( $indexs , array($date_time) ) - 1 );
}
/**
* Compare two given dates and see if the difference is less or equal 50 minutes, if so return true else false.
* @param string $data_1 YYYY/MM/DD HH:II
* @param string $data_2 YYYY/MM/DD HH:II
* @param integer $inter Interval in minutes
* @return boolean
*/
function horaComp( $date_1, $date_2, $inter )
{
$date_1_s = strtotime( $date_1 );
$date_2_s = strtotime( $date_2 );
return (bool)( abs($date_1_s-$date_2_s)<=$inter*60 );
}