1

Hi I'm trying to create a multi dimensional array but having problems. I have a multi dimensional array that I'm trying to push other arrays onto. The arrays are created and pushed within an array.

            $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;

output is like this

            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
                    )

            )

I can't get it to push the second array - I must be missing something?

I've tried everything I can think of but can't figure out why the second array (that's been created) isn't pushed onto the multi dimensional array.

Any help would be great

4
  • It would be nice to show a short example of what the original array(s) look like and what the desired array should be -- as it is hard to tell exactly what you are doing. Commented Mar 1, 2018 at 16:59
  • original array is the one shown with 12 entries the desired output is the multidimensional array with two arrays one as the one show e.g. 1, 0,29,64 and the other with 1,69,29,65 So I'm using the first number 0, 3, 6 etc as the index - initially 65 and then 66 to create the two arrays and then push those two arrays onto the multi dimension array Commented Mar 1, 2018 at 17:03
  • From the provided log it is clear that else part is never executed(because there is no message nitial child is... and initial child after change is). What are you trying to do in if(intval($selectorDetailsArray[$r]) == intval($initialChild))? Commented Mar 1, 2018 at 17:13
  • it does go to the else. I've check that. The initial array contains 12 entries. The array is 4 lots of three values. The first value is either 65 or 66. This defines which array the following values should go into. So the code loops through the 12 entry array looking at the first value. If it is 65 then it pushes the following two values into an array. When it hits the 66 value it then saves the array into the multi array and starts to create a new array Commented Mar 1, 2018 at 17:26

1 Answer 1

1

Not sure what the overall purpose of the code is for "exactly" but I do understand the results you were trying to get, and I also understand why it was not working. I have commented my code so you should be able to see what it is doing. I created an array in the code to start with, but I presume it would work with whatever array you are getting even though you are not showing how it was generated.

$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>";

This is the output that my code generates. I have added a few lines to make it readable, and also output all of the arrays at the end:

    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
            )

    )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.