1,105,281 Community Members

How to split the array values

Member Avatar
Mahesh57
Newbie Poster
10 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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..

Member Avatar
phorce
Veteran Poster
1,005 posts since Jul 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 29 [?]
Featured
 
0
 

Do you mean something like:

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

    foreach ($arr as $a)
    {
        echo $a . '<br />';
    }
?>
Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
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 />";    
}
Member Avatar
naphets
Junior Poster in Training
65 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
0
 
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article