我有一个像这样的数学阵列:
Array ( [11] => Array ( [0] => 5 )
[12] => Array ( [0] => 7 )
[14] => Array ( [0] => 9 [1] => 10 [2] => 11 [3] => 25 )
)
并希望将其转换为:
Array ( [0] => Array ( [11] => 5 )
[1] => Array ( [12] => 7 )
[2] => Array ( [14] => 9 )
[3] => Array ( [14] => 10)
[4] => Array ( [14] => 11)
[5] => Array ( [14] => 25)
)
你知道怎么做吗?
答案 0 :(得分:1)
最低限度:
foreach($myArray as $a => $b)
foreach($b as $c)
$tmpArray[] = array($a => $c);
$myArray = $tmpArray; unset($tmpArray);
答案 1 :(得分:0)
<pre>
<?php
$arr = array();
$arr[11] = array(0, 1, 2);
$arr[12] = 3;
$arr[13] = array(4, 5);
print_r($arr);
$arr2 = array();
$x = 0;
$a = 0;
while ($x < 14) {// count() will fail with '3' but our numbers are at 11
if (is_array($arr[$x])) {
$p = 0;
while ($p < count($arr[$x])) {// works here because index starts at 0
if (!is_null($arr[$x])) {
$arr2[$a++] = $arr[$x][$p];
}
$p ++;
}
}
else {
if (!is_null($arr[$x])) {
$arr2[$a++] = $arr[$x];
}
}
$x ++;
}
print_r($arr2);
?>
</pre>
结果:
Array
(
[11] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[12] => 3
[13] => Array
(
[0] => 4
[1] => 5
)
)
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)
我太累了所以这是我现在可以做的最大值.. 问题:
while ($x < 14) {// count() will be '3' but our numbers are at 11
也许这段代码可以改进..