Joe
Joe

Reputation: 415

PHP - Tricky... array into columns, but in a specific order

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

Upvotes: 0

Views: 1002

Answers (5)

bisko
bisko

Reputation: 4078

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.

Upvotes: 1

Stein G. Strindhaug
Stein G. Strindhaug

Reputation: 5119

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]);
}

Upvotes: 0

acqu13sce
acqu13sce

Reputation: 3809

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 />";
}

Upvotes: 0

Joe
Joe

Reputation: 415

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

Upvotes: 1

ahmetunal
ahmetunal

Reputation: 3960

$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>";
    }

Upvotes: 0

Related Questions