您好我试图创建一个多维数组但有问题。我有一个多维数组,我试图推动其他数组。数组在数组中创建并推送。
$initialChild = $selectorDetailsArray[0];
$selectorDetailsMultiDimArray = array();
$multiDimHoldArray = array();
for($r=0;$r<count($selectorDetailsArray);$r+=3){
echo "Test vars are ".$selectorDetailsArray[$r]." : ".$initialChild."<br> ";
if(intval($selectorDetailsArray[$r]) == intval($initialChild)){
echo"<br> r is ".$r."<br>";
array_push($multiDimHoldArray,$selectorDetailsArray[$r+1],$selectorDetailsArray[$r+2]);
echo"<br> values are ".$selectorDetailsArray[$r+1]." ".$selectorDetailsArray[$r+2]."<br>";
print "<pre>";
print_r($multiDimHoldArray);
print "</pre>";
}else{
array_push($selectorDetailsMultiDimArray,$multiDimHoldArray);
$multiDimHoldArray = array();
echo "initial child is ".$initialChild."<br>";
$initialChild = $selectorDetailsArray[$r];
echo "initial child after change is ".$initialChild."<br>";
}
}
print "<pre>";
print_r($selectorDetailsMultiDimArray);
print "</pre>";
exit;
输出就像这样
Array
(
[0] => 65
[1] => 1
[2] => 0
[3] => 65
[4] => 29
[5] => 64
[6] => 66
[7] => 1
[8] => 69
[9] => 66
[10] => 29
[11] => 65
)
Test vars are 65 : 65
r is 0
values are 1 0
Array
(
[0] => 1
[1] => 0
)
Test vars are 65 : 65
r is 3
values are 29 64
Array
(
[0] => 1
[1] => 0
[2] => 29
[3] => 64
)
Test vars are 66 : 65
initial child is 65
initial child after change is 66
Test vars are 66 : 66
r is 9
values are 29 65
Array
(
[0] => 29
[1] => 65
)
Array
(
[0] => Array
(
[0] => 1
[1] => 0
[2] => 29
[3] => 64
)
)
我无法推动推出第二个阵列 - 我一定错过了什么?
我已经尝试了我能想到的一切,但无法弄清楚为什么第二个阵列(已经创建的)没有被推到多维数组上。
任何帮助都会很棒
答案 0 :(得分:1)
不确定代码的总体目的是什么&#34;确切地说&#34;但我确实理解你想要获得的结果,我也理解为什么它不起作用。我已经对我的代码进行了评论,因此您应该能够看到它正在做什么。我在代码中创建了一个数组,但我认为即使你没有显示它是如何生成的,它也适用于你得到的任何数组。
$selectorDetailsArray = Array(65,1,0,65,29,64,66,1,69,66,29,65);
$initialChild = $selectorDetailsArray[0];
$selectorDetailsMultiDimArray = array();
$multiDimHoldArray = array();
$loopstrings = array(" :: 1st loop :: "," :: 2nd loop :: "," :: 3rd loop :: "," :: 4th loop :: ");
$loop = 0;
$dim = 0;
for($r=0;$r<count($selectorDetailsArray);$r+=3){
echo "<br>START".$loopstrings[$loop]."<br>";
echo "Test vars are ".$selectorDetailsArray[$r]." : ".$initialChild."<br> ";
if(intval($selectorDetailsArray[$r]) == intval($initialChild)){
echo"<br> r is ".$r."<br>";
array_push($multiDimHoldArray,$selectorDetailsArray[$r+1],$selectorDetailsArray[$r+2]);
echo"<br> values are ".$selectorDetailsArray[$r+1]." ".$selectorDetailsArray[$r+2]."<br>";
}else{
array_push($selectorDetailsMultiDimArray,$multiDimHoldArray);
$multiDimHoldArray = array();
// This needs to be here because the "if" is not run during this loop...
array_push($multiDimHoldArray,$selectorDetailsArray[$r+1],$selectorDetailsArray[$r+2]);
echo "initial child is ".$initialChild."<br>";
$initialChild = $selectorDetailsArray[$r];
echo "initial child after change is ".$initialChild." -- ($ r = $r)<br>";
}
echo "<br>END".$loopstrings[$loop++]."<br>";
}
// We push one last time since it successfully pushed twice more in the "if" but never ran the "else" a final time...
array_push($selectorDetailsMultiDimArray,$multiDimHoldArray);
echo "<br>";
echo "<br>BEGIN OUTPUT<br>";
print "<pre>";
print_r($selectorDetailsArray);
print_r($multiDimHoldArray);
print_r($selectorDetailsMultiDimArray);
print "</pre>";
这是我的代码生成的输出。我添加了几行以使其可读,并在最后输出所有数组:
START :: 1st loop ::
Test vars are 65 : 65
r is 0
values are 1 0
END :: 1st loop ::
START :: 2nd loop ::
Test vars are 65 : 65
r is 3
values are 29 64
END :: 2nd loop ::
START :: 3rd loop ::
Test vars are 66 : 65
initial child is 65
initial child after change is 66 -- ($ r = 6)
END :: 3rd loop ::
START :: 4th loop ::
Test vars are 66 : 66
r is 9
values are 29 65
END :: 4th loop ::
BEGIN OUTPUT
Array
(
[0] => 65
[1] => 1
[2] => 0
[3] => 65
[4] => 29
[5] => 64
[6] => 66
[7] => 1
[8] => 69
[9] => 66
[10] => 29
[11] => 65
)
Array
(
[0] => 1
[1] => 69
[2] => 29
[3] => 65
)
Array
(
[0] => Array
(
[0] => 1
[1] => 0
[2] => 29
[3] => 64
)
[1] => Array
(
[0] => 1
[1] => 69
[2] => 29
[3] => 65
)
)