0

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 ?

2 Answers 2

2

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++;
}
Sign up to request clarification or add additional context in comments.

3 Comments

suppose $colors is a key:value array is the same approach get the answer ? Thank you for the response
Yes, if the keys are arbitrary strings in the case of an associative array, keeping the variable $i as is will work.
I had updated my question @Remy Can you help on that ?
1

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.

Comments

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.