SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist Rodney H.'s Avatar
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with "Splitting" array

    Hi, I have an array of about two hundred items. I need to split them (echo them) in groups of about twenty, then echo html for a new table row, and then echo the next twenty, etc... In order to learn how to do this, I am starting small by using a simple example.

    The following code splits the array with twenty items into bite-sized morsels of five. Is there a better or easier way to do this?? (Note: the actual coded array in production won't just be integers, but will be strings.)

    PHP Code:
    <?php
    // define array of numbers
    $nums = array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");
    // counter variable
    $i 0
    while (
    $i <= 4){
    echo 
    $nums[$i] . ", ";
    $i $i 1;
    }
    echo 
    "<br>";
    while (
    $i && $i <= 9){
    echo 
    $nums[$i] . ", ";
    $i $i 1;
    }
    echo 
    "<br>";
    while (
    $i && $i <= 14){
    echo 
    $nums[$i] . ", ";
    $i $i 1;
    }
    echo 
    "<br>";
    while (
    $i 14 && $i <= 19){
    echo 
    $nums[$i] . ", ";
    $i $i 1;
    }
    ?>

  2. #2
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your while-loop (which runs through the complete Array) you can use a counter:

    if($Counter == '20'){
    print "</TD></TR> <TR><TD>";
    $Counter = '0';
    }
    else {
    $Counter++;
    }

  3. #3
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use array_chunk to split the original array into smaller "chunk" arrays, and then iterate through the chunks.

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    The way that I would do it is...

    Count the total number of things in the array and divide by whatever you want!
    In this case 2 as you only have 20 items
    PHP Code:
    <?php
    $nums 
    = array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"); 
    // counter variable 
    $i 0
    while (
    $i <= count($nums)/2){ 
    echo 
    $nums[$i] . ", "
    $i $i 1

    ?>
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Evangelist Rodney H.'s Avatar
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOW! Thanks, everyone. I learned a lot. (ended up using "array_chunk") cos it was the easiest way to "split" array into pieces of eighteen.

    Take care, and thanks again. I appreciate it.

    -R

  6. #6
    SitePoint Zealot rain5017's Avatar
    Join Date
    Sep 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $nums 
    = array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"); 
    $num_count=count($nums);
    for(
    $i=0;$i<$num_count;$i++){

    echo 
    $nums[$i] . ", "
    if((
    $i+1)%5==0)echo '<br>';
    }

    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •