0
<?php

$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");


        $num_cols = 3;

        $i = 0;
        foreach ($combinedArray as $r ){
            /*** use modulo to check if the row should end ***/
            echo $i++%$num_cols==0 ? '<div style="clear:both;"></div>' : '';
            /*** output the array item ***/
    ?>
        <div style="float:left; width:33%;">
    <?php
            echo $r;
    ?>
        </div>
    <?php
        }
    ?>
    <div style="clear:both;"></div>

The above code will print out the array like this:

apple --- banana --- watermelon

lemon --- orange --- mango

However, I need it like this:

apple --- watermelon --- orange

banana --- lemon --- mango

Do you know how to convert this? Basically, each value in the array needs to be placed underneath the one above, but it must be based on this same structure of 3 columns, and also an equal amount of fruits per column/row (unless there was like 7 fruits there would be 3 in one column and 2 in the other columns.

Sorry I know it's confusing lol

1
  • One idea would be to resort the array and then use the above function to display them... although the math is confusing me Commented Mar 24, 2010 at 6:36

5 Answers 5

1

Thanks everyone for your help... I realized a better way to do it though. Simple put, I have 3 columns floating next to eachother. And in each column, I add a list of the items into it and stop when I hit the max items per row.

working code:

<div style="float:left; width:33%;">
<?php

        $combinedArraySizeOf = sizeof($combinedArray);
        $num_cols = 3;
        $iPerRow = $combinedArraySizeOf / $num_cols;

        for ($i=0; $i!=$combinedArraySizeOf; $i++){

            if ($i % $iPerRow == 0){
                echo '</div><div style="float:left; width:33%;">';
            }

            echo $combinedArray[$i]."<br />";

        }
?>
</div>
<div style='clear:both;'></div>

Don't forget to clear both at the end if necessary :P

Sign up to request clarification or add additional context in comments.

2 Comments

Well, that is the solution from bisko.
YES sorry my mistake.. thanks bisko! (although his version is less neat and also contained a bug so I rewrote it from scratch w/o even thinking of his but yes I guess it's the same logic!)
1

Why aren't you doing exactly what you want to do? I mean show them in columns, instead of rows?

$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");
$num_cols = 3;
$rowCount = ceil(count($combinedArray)/$num_cols);
$i = 1; // in order the modulus to work correctly

?>
<div style="float: left; width:33%"> <!-- this is the first column -->

foreach ($combinedArray as $r ){
   ?>

   <div> <!-- just a div containing $r -->
      <?php
         echo $r;
      ?>
   </div>
<?php

   // this is where the magic happens
   // check if we have enough rows and start another column
   if ($i % $rowCount == 0) {
      ?>
      </div> <!-- close the previous column and start a new one -->
      <div style="float: left; width:33%"> <!-- this is the new column -->
      <?php
   }

   $i++;
}
?>
</div> <!-- closing the last open column -->
<div style="clear:both;"></div>

This should do just the job you wish. Marvin's answer is better if you want to use only tables without divs.

3 Comments

I tried it... maybe I'm missing something, but they are all appearing in one column.
even after adding the $i++ which was missing, this doesn't work correctly I don't think this has the right logic
My bad for skipping the $i++; May be I was too sleepy :) The code works for me though ?
0
$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");

    $step = 2;
    $i = 0;
    $new_array = array();

    foreach ($combinedArray as $r ){
        $remainder = ($i % $step);
        $new_array[$remainder][] = $r;
        $i++;
    }

    foreach($new_array as $array)
    {
        echo implode(' --- ', $array)."<br>";
    }

3 Comments

I tested this, it doesn't appear to work correctly. The order is messed up?
but yes.. it does work for the above example, without the floats in divs though :(
instead of using implode, you can use another foreach to add floated divs, i was just being lazy
0

Would this work?

$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");
$num_cols = 3;
$rows = ceil(count($combinedArray)/$num_cols);

for($i = 0; $i < $rows; $i++){
    for($j = 0; $j < $num_cols; $j++){
        echo $combinedArray[(($i+$j) * $rows)-$i];
    }
echo "<br />";
}

This would also need to check that the value existed for cases where the number of items wasn't precisely divisible by the number of columns, you could do that with the following change:

$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");
$num_cols = 3;
$rows = ceil(count($combinedArray)/$num_cols);

for($i = 0; $i < $rows; $i++){
    for($j = 0; $j < $num_cols; $j++){
        $cell = (($i+$j) * $rows)-$i;
        if($cell > count($combinedArray)) break;
        echo $combinedArray[$cell];
    }
    echo "<br />";
}

2 Comments

It worked at killing my browser. lol.. anyway the page froze with this som reason
oops, my bad. I was incrementing $i in both loops, now fixed and it seems to produce what you want
0

If this order is what you ideally want, but it's not critical that it works in all browsers, perhaps you should look at coloumn layout (still very experimental css3 draft). If you use dispay inline block for the element in each coloumn you'll have the current order as a fallback.

You could also use a table for the layout and use a for loop something like this (pseudo php code, it's been a while since I've coded any php):

maxHeight = Math.ceil(array.length / 3) // meaning length=6 will give 3, 
                                         // length=7 will give 4
$x = -1; // horizontal index
for(i = 0; i < array.length(); i++){
   $y = i % maxHeight; // vertical index 
   if($y == 0){
      $x++;
   }
   addToTable($x,$y, array[i]);
}

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.