1.11M Members

How to split the array values

 
0
 

I have an array

array('mango','apple','orange','bat','cat','mat');

like it for example

i want to show an output in rows and columns like as follows

mango apple orange
bat cat mat

how to get output show an above..

 
0
 

Do you mean something like:

<?php
    $arr = array('mango','apple','orange','bat','cat','mat');

    foreach ($arr as $a)
    {
        echo $a . '<br />';
    }
?>
 
1
 

You seem to want to split after a certain number of items. Do you want each item to be in its own cell in a table or just on a row?

Here are 2 ideas that came to mind, There must be many more:

<?php
$arr = array("A","B","C","D","E","F");
$step = 3;


echo '<h3>METHOD 1: array_chunk(), implode() - plain text</h3>';
//METHOD 1
$arrs = array_chunk($arr, $step);
foreach($arrs as $line) echo implode(' ', $line) . '<br />';

echo '<h3>METHOD 2: loop and check modulo (%) - plain text</h3>';
//METHOD 2
for($x=0;$x<count($arr);$x++){
    echo $arr[$x] .' ';
    if(($x+1) % $step == 0)echo "<br />";    
}
 
0
 

this is exactly what i am working on myself... :)
In my case, i am wanting... the output of a sql query to populate cells in a table.
each sql query results in the need for a different table format, complicating everything.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article