PHP for loop over array acting weird..



  • I get three inputs from a submitted form:

    1. The amount of sections
    2. The amount of items in each sections (array)
    3. All the items (array)

    The form loop starts with looping over the sections

    for($r = 0; $r < $sectionamount; $r++) {
    

    Then it have an inside for loop to loop over each item in the section

    for( $ret = 0; $ret < $hoevelbinee; $ret++ ) {
    	 
    	 $diwordeid = $myform[$fiv];
    	 
    	$diou .= "die input is " . $diwordeid . "</br>";
    	$fiv++;
     }
    

    So the complete function would be

    $tr = 0;
    $fiv = 0;
    
    for($r = 0; $r < $hoevelsections; $r++) {
    	
    $hoevelbinee = $myformsekis[$tr];
    $ret = 0;
    
     for( $ret = 0; $ret < $hoevelbinee; $ret++ ) {
    	 
    	 $diwordeid = $myform[$fiv];
    	 
    	$diou .= "die input is " . $diwordeid . "</br>";
    	$fiv++;
     }
    
    $tr++;
    
    }
    

    If I have two sections with each two items, it works perfectly e.g.

    Array
    (
        [0] => Entrance
        [1] => Door & Door frame
        [2] => Kombuis
        [3] => Door & Door frame
    )
    
    Array
    (
        [0] => 2
        [1] => 2
    )
    
    Output:
    
    die input is Entrance
    die input is Door & Door frame
    die input is Kombuis
    die input is Door & Door frame
    

    But anything more that two sections, the for loop would miss the last two or three inputs e.g.

    Array
    (
        [0] => Entrance
        [1] => Door & Door frame
        [2] => Kombuis
        [3] => vloer
        [4] => kaste
        [5] => Kamer
        [6] => gang
        [7] => bed
        [8] => mat
    )
    
    Array
    (
        [0] => 2
        [1] => 2
        [2] => 3
    )
    
    Output:
    
    die input is Entrance
    die input is Door & Door frame
    die input is Kombuis
    die input is vloer
    die input is kaste
    die input is Kamer
    die input is gang
    

    So why would the for loop not do every entry in the array?



  • @jimmi

    What is $sectionamount ? Is that the same as $hoevelsections ?

    What is the purpose of it? Are you trying to print all items in the last array you listed:

    Array
    (
        [0] => Entrance
        [1] => Door & Door frame
        [2] => Kombuis
        [3] => vloer
        [4] => kaste
        [5] => Kamer
        [6] => gang
        [7] => bed
        [8] => mat
    )