数组与 foreach 的合并组合
Array merge combination with foreach
需要进行哪些阵列管理操作?
所以,有两个数组,我想按以下方式组合:
$arr
Array
(
[0] => 2015-08-16 22:12:04
[1] => 2015-08-16 13:20:17
[2] => 2015-08-16 11:45:47
[3] => 2015-08-16 02:35:12
[4] => 2015-08-15 19:05:02
[5] => 2015-08-15 17:35:12
[6] => 2015-08-15 09:02:25
[7] => 2015-08-15 07:12:00
[8] => 2015-08-14 22:12:04
[9] => 2015-08-14 13:20:17
[10] => 2015-08-14 11:45:47
)
其他数组($arr2)
Array
(
[2015-08-16 22:12:04] => 4.8
[2015-08-16 13:20:17] => 5.8
[2015-08-16 11:45:47] => 4.7
[2015-08-16 02:35:12] => 2.8
[2015-08-15 19:05:02] => 5.0
[2015-08-15 17:35:12] => 3.0
[2015-08-15 09:02:25] => 5.6
[2015-08-15 07:12:00] => 4.0
[2015-08-14 22:12:04] => 4.8
[2015-08-14 13:20:17] => 5.8
)
我想要以下输出:
$arr3
Array
(
2015-08-16 => Array
(
[2015-08-16 22:12:04] => 4.8
[2015-08-16 13:20:17] => 5.8
[2015-08-16 11:45:47] => 4.7
[2015-08-16 02:35:12] => 2.8
)
2015-08-15 => Array
(
[2015-08-15 19:05:02] => 5.0
[2015-08-15 17:35:12] => 3.0
[2015-08-15 09:02:25] => 5.6
[2015-08-15 07:12:00] => 4.0
)
2015-08-14 => Array
(
[2015-08-14 22:12:04] => 4.8
[2015-08-14 13:20:17] => 5.8
[2015-08-14 11:45:26] => 4.4
)
)
到目前为止我得到了:
(整个就是一个for循环)
$ts = strtotime($year.'W'.$week.$i);
$thedates = date("Y-m-d", $ts);
$input = preg_quote($thedates, '~');
$input = str_replace("\-","-",$input);
$result = preg_grep('~' . $input . '~', $arr);
$a = array(
$thedates=>$result,
);
输出:
Array
(
[1439510400] => Array
(
[8] => 2015-08-14 22:12:04
[9] => 2015-08-14 13:20:17
[10] => 2015-08-14 11:45:47
[11] => 2015-08-14 02:35:12
)
)
Array
(
[1439596800] => Array
(
[4] => 2015-08-15 19:05:02
[5] => 2015-08-15 17:35:12
[6] => 2015-08-15 09:02:25
[7] => 2015-08-15 07:12:00
)
)
Array
(
[1439683200] => Array
(
[0] => 2015-08-16 22:12:04
[1] => 2015-08-16 13:20:17
[2] => 2015-08-16 11:45:47
[3] => 2015-08-16 02:35:12
)
)
一个简单的 foreach 循环就可以完成您想要的操作。只是 explode()
space 的键,所以你有日期和时间分隔,例如
<?php
$result = [];
foreach($arr2 as $k => $v){
list($date, $time) = explode(" ", $k);
$result[$date][$k] = $v;
}
?>
需要进行哪些阵列管理操作? 所以,有两个数组,我想按以下方式组合:
$arr
Array
(
[0] => 2015-08-16 22:12:04
[1] => 2015-08-16 13:20:17
[2] => 2015-08-16 11:45:47
[3] => 2015-08-16 02:35:12
[4] => 2015-08-15 19:05:02
[5] => 2015-08-15 17:35:12
[6] => 2015-08-15 09:02:25
[7] => 2015-08-15 07:12:00
[8] => 2015-08-14 22:12:04
[9] => 2015-08-14 13:20:17
[10] => 2015-08-14 11:45:47
)
其他数组($arr2)
Array
(
[2015-08-16 22:12:04] => 4.8
[2015-08-16 13:20:17] => 5.8
[2015-08-16 11:45:47] => 4.7
[2015-08-16 02:35:12] => 2.8
[2015-08-15 19:05:02] => 5.0
[2015-08-15 17:35:12] => 3.0
[2015-08-15 09:02:25] => 5.6
[2015-08-15 07:12:00] => 4.0
[2015-08-14 22:12:04] => 4.8
[2015-08-14 13:20:17] => 5.8
)
我想要以下输出: $arr3
Array
(
2015-08-16 => Array
(
[2015-08-16 22:12:04] => 4.8
[2015-08-16 13:20:17] => 5.8
[2015-08-16 11:45:47] => 4.7
[2015-08-16 02:35:12] => 2.8
)
2015-08-15 => Array
(
[2015-08-15 19:05:02] => 5.0
[2015-08-15 17:35:12] => 3.0
[2015-08-15 09:02:25] => 5.6
[2015-08-15 07:12:00] => 4.0
)
2015-08-14 => Array
(
[2015-08-14 22:12:04] => 4.8
[2015-08-14 13:20:17] => 5.8
[2015-08-14 11:45:26] => 4.4
)
)
到目前为止我得到了: (整个就是一个for循环)
$ts = strtotime($year.'W'.$week.$i);
$thedates = date("Y-m-d", $ts);
$input = preg_quote($thedates, '~');
$input = str_replace("\-","-",$input);
$result = preg_grep('~' . $input . '~', $arr);
$a = array(
$thedates=>$result,
);
输出:
Array
(
[1439510400] => Array
(
[8] => 2015-08-14 22:12:04
[9] => 2015-08-14 13:20:17
[10] => 2015-08-14 11:45:47
[11] => 2015-08-14 02:35:12
)
)
Array
(
[1439596800] => Array
(
[4] => 2015-08-15 19:05:02
[5] => 2015-08-15 17:35:12
[6] => 2015-08-15 09:02:25
[7] => 2015-08-15 07:12:00
)
)
Array
(
[1439683200] => Array
(
[0] => 2015-08-16 22:12:04
[1] => 2015-08-16 13:20:17
[2] => 2015-08-16 11:45:47
[3] => 2015-08-16 02:35:12
)
)
一个简单的 foreach 循环就可以完成您想要的操作。只是 explode()
space 的键,所以你有日期和时间分隔,例如
<?php
$result = [];
foreach($arr2 as $k => $v){
list($date, $time) = explode(" ", $k);
$result[$date][$k] = $v;
}
?>