我在尝试以特定方式展平数组时遇到了一些小麻烦。
这是我想要压平的阵列的print_r
视图:
Array
(
[1] => Array
(
[8] => 1
[9] => 2
[10] => Array
(
[15] => Array
(
[22] => 1
)
[21] => 2
)
[11] => Array
(
[16] => Array
(
[23] => 1
)
)
)
[2] => Array
(
[12] => 1
)
[3] => Array
(
[13] => 1
)
[4] => Array
(
[14] => 1
)
[5] => 5
[6] => 6
[7] => 7
)
我试图创建的是一个数组,它保持上面的索引,但值等于它在数组中的位置,很像原始索引(从零开始(。
这是想要的结果:
Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 1
[9] => 2
[10] => 3
[11] => 4
[12] => 1
[13] => 1
[14] => 1
[15] => 1
[16] => 1
[21] => 2
[22] => 2
[23] => 1
)
众所周知,有17至20人失踪。
我的功能如下:
function array_flatten ($array) {
$result = array ();
$count = 1;
while ($index = current($array)) {
$result[key($array)] = $count;
if (is_array($index)) {
$result = array_merge(array_flatten($index), $result);
}
next($array);
$count++;
}
return $result;
}
线路$result = array_merge(array_flatten($index), $result);
似乎是问题所在。它返回:
Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
)
但是,如果我在同一行上运行var_dump(array_flatten($index));
,它会将我希望合并的所有数组返回到$result
变量中。
array
22 => int 1
array
15 => int 1
21 => int 2
array
23 => int 1
array
16 => int 1
array
8 => int 1
9 => int 2
10 => int 3
11 => int 4
array
12 => int 1
array
13 => int 1
array
14 => int 1
看起来array_merge
实际上并没有合并这些数组。
我做错什么了吗?任何指导意见都将不胜感激。非常感谢。
更新
解决了!
完成所需功能如下:
function array_flatten($array, &$result = array()) {
$count = 1;
foreach($array as $index => $value) {
$result[$index] = $count;
if(is_array($value)) {
array_flatten($value, $result);
}
$count++;
}
return $result;
}
function flatten_array($array, &$result) {
foreach($array as $key => $value) {
if(is_array($value)) {
flatten_array($value, $result);
} else {
$result[$key] = $value;
}
}
}
要使用它,请检查下面的示例代码:
$flattened = array();
$test = array(
1 => 1
, 3 => 2
, array(2 => 4, 4 => 6)
, 5 => 3
, array(7 => 9, 8 => 7, 9 => 5)
);
flatten_array($test, $flattened);
// Now $flattened contains the flattened array
在你澄清了你的问题后,我有点惊讶你接受了一个没有返回你期望的数据的答案。(我现在看到你在你的问题中添加了你的解决方案。(
我所做的:我以@Arjan的函数为基础,在问题输入数据上运行它,并将其与问题预期数据进行比较。然后我做了一点。这就是我(因此(想到的:
# COMP EXPECTED . ACTUAL
#00: == Array . Array
#01: == ( . (
#02: == [1] => 1 . [1] => 1
#03: == [2] => 2 . [2] => 2
#04: == [3] => 3 . [3] => 3
#05: == [4] => 4 . [4] => 4
#06: == [5] => 5 . [5] => 5
#07: == [6] => 6 . [6] => 6
#08: == [7] => 7 . [7] => 7
#09: == [8] => 1 . [8] => 1
#10: == [9] => 2 . [9] => 2
#11: == [10] => 3 . [10] => 3
#12: == [11] => 4 . [11] => 4
#13: == [12] => 1 . [12] => 1
#14: == [13] => 1 . [13] => 1
#15: == [14] => 1 . [14] => 1
#16: == [15] => 1 . [15] => 1
#17: == [16] => 1 . [16] => 1
#18: == [21] => 2 . [21] => 2
#19: != [22] => 2 . [22] => 1
#20: == [23] => 1 . [23] => 1
#21: == ) . )
#22: == .
看起来你的预期数据在位置22上有一个错误。
这是修改后的功能(演示(:
function flatten_array($array, &$result = null) {
$r = null === $result;
$i = 0;
foreach($array as $key => $value) {
$i++;
if(is_array($value)) {
$result[$key] = $i;
flatten_array($value, $result);
} else {
$result[$key] = $value;
}
}
if ($r) {
ksort($result);
return $result;
}
}
$actual = flatten_array($input);