I have an array


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..

Recommended Answers

All 4 Replies

Do you mean something like:

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

    foreach ($arr as $a)
        echo $a . '<br />';
Member Avatar

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:

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

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.