Display array items in groups

622 views Asked by At

I want to group items in a while loop (wrap multiple items in a div) so that first item is in one div, and rest of the items are in the set of 4 items in each div.

The problem is that the amount of the items varies. I have tried multiple ways using the if condition within the loop, but the problem occurs if the number of items in the loop are more or less than expected. Then it just either misses or adds an additional div

For example, if I have 10 items, I'm trying to achieve this output:

<div class="group1">
item1
</div>

<div class="group2">
item2
item3
item4
item5
</div>

<div class="group3">
item6
item7
item8
item9
</div>

<div class="group4">
item10
</div>

How that can be done regardless

3

There are 3 answers

1
Mukesh Prajapat On BEST ANSWER

Use this :

$array = array('item1','item2','item3','item4','item5','item6','item7','item8','item9','item10');
$i = 1;
$j = 1;
echo $div = '<div class="group'.$j.'">';
foreach ($array as $value) {
    if($i==1){
        echo $value.'<br/>';
        echo '</div>';
    }elseif($i%4==2){
        $j++;
        echo '</div>

        <div class="group'.$j.'">';
        echo $value.'<br/>';
    }else{
        echo $value.'<br/>';
    }
    $i++;
}
0
Qaisar Satti On

You will get output by this code, add your condition.

    <?php
$array=['item1','item2','item3','item4','item5','item6','item7','item8','item9','item10'];
$i=1;
$j=1;
foreach($array as $v){
    if($i==1){
        echo '<div class="group'.$j.'">'.$v;echo '</div>';
         $j++;
         echo '<div class="group'.$j.'">';
    }elseif($i==count($array)){
        $j++;
        echo '</div>';
        echo '<div class="group'.$j.'">'.$v.'</div>';
    }elseif($i%6==0){
        $j++;
       echo '</div>';
        echo '<div class="group'.$j.'">';
    }else {
        echo $v.'-';
    }

    $i++;
}

Output

<div class="group1">item1</div>
<div class="group2">item2-item3-item4-item5-</div>
<div class="group3">item7-item8-item9-</div>
<div class="group4">item10</div>
0
mickmackusa On

Here is a mostly functional approach using current(), array_slice(), array_chunk(), and implode() that avoids using a modulus arithmetic operator to generate the correct structure and group numbering:

Code: (Demo)

$array=['item1','item2','item3','item4','item5','item6','item7','item8','item9','item10'];

// display 1st element on its own
echo '<div class="group1">',current($array),'</div>';

// group elements (2nd to end) in sets upto 4
foreach(array_chunk(array_slice($array,1),4) as $i=>$chunk){
    echo '<div class="group',$i+2,'">',implode('<br>',$chunk),'</div>';
}

Output:

<div class="group1">item1</div>
<div class="group2">item2<br>item3<br>item4<br>item5</div>
<div class="group3">item6<br>item7<br>item8<br>item9</div>
<div class="group4">item10</div>

*note, the above code assumes $array will not be empty. If it is possible for $array to have zero elements in it, then my processing lines should be wrapped in a condition that checks for 1 or more elements.


Here is a method that uses conditionals, a modulus, and a little arithmetic to generate the correct structure and group numbering:

Code: (Demo)

foreach($array as $i=>$v){
    if(!$i){                                       // $i is zero
        echo '<div class="group1">';               // first group
    }elseif($i%4==1){                              // $i is 1, 5, 9, etc.
        echo '</div>';                             // close previous group
        echo '<div class="group',(--$i/4)+2,'">';  // $i minus 1, divided by 4, plus 2
    }else{
        echo '<br>';                               // the "glue" between values
    }
    echo $v;                                       // display the value
}
echo '</div>';                                     // assumes the array is never empty...
                             // otherwise, finish with:  if(sizeof($array)){echo '</div>';}

Output: [ same as first method ]