0

PHP novice here. I would like to ask for a bit of help with my nested loop. I think I'm close but I'm pretty sure what I'm missing is a switch or a break or both. I've messed with this for a while but I just cant get it right. Here is the code example.

<?php $items=array(thing01,thing02,thing03,thing04,thing05,thing06,thing07,thing08,thing09,thing10,thing11,thing12,thing13,thing14,thing15,thing16,thing17,thing18,thing19,thing20,thing21,thing22,thing23,thing24,thing25,thing26,thing27,thing28,thing29,thing30,thing31,thing32); ?>
<?php $array_count = count($items); ?>
<?php $item_count = 9; ?>
<?php $blk_Number = ceil( $array_count / $item_count); ?>
<?php echo "<h3>This list should contain " . $array_count . " items</h3>"; ?>
<ul>
<?php
for ($pas_Number = 1; $pas_Number <= $blk_Number; $pas_Number++) {print "<h3>Start of Block " . $pas_Number . " of 9         items</h3>";
for ($key_Number = 0; $key_Number < $item_count; $key_Number++){print "<li>" . $items[$key_Number] . "</li>"; }
{print "<h3>End of Block " . $pas_Number . " of 9 items</h3>"; }
}
; ?>
</ul>

This is giving me the output of:

This list should contain 32 items

Start of Block 1 of 9 items
thing01
thing02
thing03
thing04
thing05
thing06
thing07
thing08
thing09
End of Block 1 of 9 items
Start of Block 2 of 9 items
thing01
thing02
thing03
thing04
thing05
thing06
thing07
thing08
thing09
End of Block 2 of 9 items
Start of Block 3 of 9 items
thing01
thing02
thing03
thing04
thing05
thing06
thing07
thing08
thing09
End of Block 3 of 9 items
Start of Block 4 of 9 items
thing01
thing02
thing03
thing04
thing05
thing06
thing07
thing08
thing09
Start of Block 4 of 9 items
thing01
thing02
thing03
thing04
thing05
thing06
thing07
thing08
thing09
End of Block 4 of 9 items

As you can see the count of the Array elements is wrong. Block 2 should contain things 10-18, Block 3 should contain things 19-27, and Block 4 should contain the remaining 5 "things". I apologize about all of the silly elements in the array, but I wanted to be able to clearly explain what I'm trying to do.

2 Answers 2

2

I think you want to use array_chunk():

foreach (array_chunk($items, 9) as $nr => $block) {
    echo "Block $nr\n";
    foreach ($block as $item) {
        echo "\t$item\n";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wow...never knew about array_chunk It worked perfectly! thanks
1

Replace

for ($key_Number = 0; $key_Number < $item_count; $key_Number++){print "<li>" . $items[$key_Number] . "</li>"; }

with

for ($key_Number = 0; $key_Number < $item_count && $key_number + $pas_number * $item_count < $array_count; $key_Number++){print "<li>" . $items[$key_Number + $pas_number * $item_count] . "</li>"; }

Currently, you're getting the same results on the every outer loop iteration because your inner loop does not depend on the iteration of outer loop.

2 Comments

hmmm...that change resulted in all of the inner elements going away
@DavidRamirez I fixed a mistype.

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.