我的数组
Array
(
[1] => Array
(
[19-05-18] => 9:00
)
[2] => Array
(
[19-05-18] => 9:00
)
[3] => Array
(
[19-05-18] => 9:06
)
[4] => Array
(
[19-05-18] => 9:10
)
[5] => Array
(
[19-05-18] => 9:10
)
[9] => Array
(
[22-05-18] => 18:23
)
[10] => Array
(
[22-05-18] => 18:25
)
[11] => Array
(
[22-05-18] => 18:25
)
[12] => Array
(
[22-05-18] => 18:27
)
[13] => Array
(
[22-05-18] => 18:27
)
[14] => Array
(
[22-05-18] => 18:23
)
)
但我想要像这样的输出
Array
(
[1] => Array
(
[19-05-18] => 9:00
[19-05-18] => 9:06
[19-05-18] => 9:10
)
[2] => Array
(
[22-05-18] => 18:23
[22-05-18] => 18:25
[22-05-18] => 18:27
)
)
答案 0 :(得分:0)
$new = array();
foreach ($arr as $val) {
foreach ($val as $k => $v) {
if (!isset($new[$k]))
$new[$k] = array();
if (!in_array($v, $new[$k])) {
$new[$k][] = $v;
}
}
}
你可以有这样的东西。
它会将时间存储在一个数组数组中,并将时间作为新索引 第一个if语句将确保没有重复的条目。当然,如果您想要重复的条目,那么非常欢迎您从代码中删除该语句。
答案 1 :(得分:0)
对数组进行分组的一种方法是使用array_reduce
$arr = .... //Your array
$result = array_reduce( $arr, function( $c, $v ){
$c[ key( $v ) ][] = current( $v );
return $c;
}, array() );
echo "<pre>";
print_r( $result );
echo "</pre>";
这将导致:
Array
(
[19-05-18] => Array
(
[0] => 9:00
[1] => 9:00
[2] => 9:06
[3] => 9:10
[4] => 9:10
)
[22-05-18] => Array
(
[0] => 18:23
[1] => 18:25
[2] => 18:25
[3] => 18:27
[4] => 18:27
[5] => 18:23
)
)
Doc:array_reduce()