Gopipuli
Gopipuli

Reputation: 393

How can I add value after some specific count in foreach loop of php?

My code is

<?php
$colors = array("red", "green", "blue", "yellow", "violet");
$i=0;
foreach ($colors as $value) {
  echo "$value.$i <br>";
  $i++;
}
?>

gives a result as

red.0
green.1
blue.2
yellow.3
violet.4

Now I want my result to be like this

section
red.0
green.1
section
blue.2
yellow.3
section
violet.4

adding a value after each two rows until my array finishes.? How can I get this done ? thanks in advance.

I try the code with two foreach loop is

$i = 0;
    foreach ($strData->body as $ins) {
        $items = [];
        if ($i % 2 == 0) {
            // We will echo section at the first iteration and every multiple of 2 now.
            fwrite($fp, str_replace('</pre>','', str_replace('<pre >','',''. $finalHead . $table->getTable() )));
        }
        foreach ($ins as $key=>$value) {
            //echo "-----".$value."-----";
            array_push($items,$value);
        }
        $table->addRow($items);

        $i++;
    }

result is

      ** ADT PLASTIC ** STATEMENT as on 12/08/2021   
   
--------------
 ** ADT PLASTIC ** STATEMENT as on 12/08/2021   
                  
----------------------------
 CC Limit       1,000 
 Availed Limit  10,621   
 ** ADT PLASTIC ** STATEMENT as on 12/08/2021   
                   
-----------------------------
 CC Limit        1,000 
 Availed Limit   10,621   
 PM Stock Value  75,095   
 RM Stock Value  67,895   
 ** ADT PLASTIC ** STATEMENT as on 12/08/2021   
                        
----------------------------------
 CC Limit             1,000 
 Availed Limit        10,621   
 PM Stock Value       75,095   
 RM Stock Value       67,895   
 Product Stock Value  46,456   
 Total Stock Value    1,446 
 *Report taken by NATH on 19/08/2021 at 04:08:34 in TESTER * 

   

Actualy I want that heading only after each two rows only. Where am wrong ?

Upvotes: 0

Views: 101

Answers (2)

jspit
jspit

Reputation: 7683

The following solution uses array_chunk() to divide the array into groups.

$colors = array("red", "green", "blue", "yellow", "violet");
foreach(array_chunk($colors, 2, true) as $secNo => $group){
  echo "section $secNo<br>";
  foreach($group as $no => $value){
    echo $value.".".$no."<br>";
  }  
}

Output:

section 0
red.0
green.1
section 1
blue.2
yellow.3
section 2
violet.4

$secNo is an extra. Can be left out if not desired.

If array_chunk becomes array_chunk($colors, 2, false) used the numbering of the colors in each section starts at zero.

Upvotes: 1

Remy
Remy

Reputation: 807

You can achieve this by using for example the modulo operator %. This way you can test if the value of $i is a multiple of 2 or by including the $key (index in the foreach loop and test if $key is a multiple of 2).

Alternatively you could also use a for loop for the same result.

Take a look at the example using your code:

$colors = array("red", "green", "blue", "yellow", "violet");
$i = 0;

foreach ($colors as $value) {
    if ($i % 2 == 0) {
        // We will echo section at the first iteration and every multiple of 2 now.
        echo "section <br>";
    }

    echo "$value.$i <br>";
    $i++;
}

Upvotes: 2

Related Questions