0

enter image description here

Hey GUys Im beginner to php programming here here i have 15 element in array . i want to display first 4 array element in first line and then second 4 array of element in nextline . i dont know how to achive it here is my code help me on this. thanks in advance <?php

$arry=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o');

echo $nr_elm = count($arry);        // gets number of elements in $arry
$nr_col = 4;       // Sets the number of text Per Line

 

// If the array has elements
    if ($nr_elm > 0) 
    {
        // Traverse the array with FOR
        for($i=0; $i<$nr_elm; $i++) 
        {
            echo $textInLine= $arry[$i]. '&nbsp;|&nbsp;'; 
            // If the number of columns is completed for a line (rest of division of ($i + 1) to $nr_col is 0)
            // Closes the current line, and begins another line
            $col_to_add = ($i+1) % $nr_col;
            if($col_to_add == 0) { $textInLine .= '/n'; }
        }
        
    }
echo $textInLine;  

?>
3
  • /n isn't a valid line break character. If you're outputting to HTML (i.e. a web page) then use the HTML line break <br/>. If you're outputting to console then use \n (backslash not forward slash). Commented Sep 19, 2020 at 7:41
  • thanks for suggesting but still not get desire result Commented Sep 19, 2020 at 7:52
  • well I didn't say that was the whole solution, just one small mistake :-) Commented Sep 19, 2020 at 7:55

4 Answers 4

3

Use array_chunk for this:

$array = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o');
$size  = 4;

foreach (array_chunk($array, $size) as $chunk) {
    echo implode(' ', $chunk) . PHP_EOL;
}

Another solution without using array_chunk is using modulo:

$array   = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o');
$size    = 4;
$counter = 0;
foreach ($array as $character) {
    echo $character;

    // echo new line after every 4th character, a space after the others
    echo (++$counter % $size === 0) ? PHP_EOL : ' ';
}
Sign up to request clarification or add additional context in comments.

3 Comments

Your modulo code should probably have the echo ' '; in the else. Not that it's a big deal but you add a space on each line after the four items.
You already have the if, you just need to add the else{}
Also a way of doing it :-)
1

Chunk the array and implode the items in the chunk.

$arry=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o');
$chunks = array_chunk($array, 4);

foreach($chunks as $chunk){
    echo implode(" ", $chunk) . "</br>\n";
}

Array_chunk splits the array in to pieces of the size you define.
The resulting array is multidimensional with the items in the subarray.

Implode takes the items in the subarray and adds the delimiter (" ") in between each item and makes it a string.

Comments

0

Use array_chunk, it will Split an array into chunks

  <?php    
            $arry=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o');
            $arr = array_chunk($arry, 4, true);
            
            foreach($arr as $value) {
                echo implode(' ', $value) . PHP_EOL ;
            } 
      ?>

Comments

0

I don't know if the "15" is intentional, but if you want to remove it just remove the "echo".

I also recommend to use empty() to verify if your array is empty or not, just like this :

if (!empty($arry)) { //Code } (notice the "!")

Now, for your main question what I would do is using a variable which increments up to 4, then you insert a line break with echo <br>; and reset the variable to 0.

The code should look something like this :

$c = 0;
for($i=0; $i<$nr_elm; $i++) 
{

    echo $arry[$i];
    $c ++;
    if ($c >= 4) {
        echo "<br>";
        $c = 0;
    } 

}

I hope this helped you

2 Comments

>= 4 more than? How will that happen? :-)
@Andreas I don't know, I do that often, in case things go wrong

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.