I have products which they will be shown horizontally in Grid page,however I want them to be sorted vertically and I have 4 coulmns
if we had a simple array the out put is

1  2 3 4
5  6 7 8
9 10 11 12

I want the output to be

1 4 7 10
2 5 8 11
3 6 9 12

I wrote a test page without magento a simple array, I want to do this on array of products but I really don't know how to do it? because I have array of products that will be loaded and I should change the order of them. do you have any other idea?

thanks in advance

for($m=0; $m<4;$m++)
     * build the remainders
        //print the result
echo "<table>";
echo "<tr>";


    if($i==4 && $j<$r)
            echo "<td>".$arr[$i][$j]."</td>";
        else if($i!=4)
        echo "<td>".$arr[$i][$j]."</td>";   
        echo "</tr>";
echo "</table>";
echo "arraysize is ".$count."<br>";
echo "division answer is".$k."<br>";;
echo "remainder is".$r;

here is the list.phtml part

       <ol class="products-list" id="products-list">
        <?php foreach ($_productCollection as $_product): 

so the $_product array should be displayed vertically

                <?php // Product Image ?> 
                <a href="<?php echo $_product->getProductUrl() ?>"
//some other code

1 Answer 1


It's not so difficult to do, actually. It helps to think about this outside of the context of Magento as you did above; but your method could be simplified1, 2:


$cols       = 5; //set this to the # columns you want
$count      = 55;//number of total records
$list       = range(1,$count);
$rowLength  = ceil($count/$cols);
$array      = array();

$output = array_chunk($list, $rowLength);

    @array_walk($output,function($val) use ($i){
        echo $val[$i] . ' ';
    echo PHP_EOL;

Which, when calculated for 5 columns and 32 count you get:

1 8 15 22 29
2 9 16 23 30
3 10 17 24 31
4 11 18 25 32
5 12 19 26
6 13 20 27
7 14 21 28

All that is happening here is that the $list array (a stand-in for your Magento collection) is getting chunked into multiple arrays sized by $rowLength. After that all we're doing is iterating through all the columns and outputting the row index at that column value! That's pretty cool.

enter image description here

Do this in Magento

Load up the collection and cast to an array:

$collection = Mage::getModel('catalog/product')->getCollection()->setPageSize(20);
$array = $collection->toArray();

Then we plug it into our method that uses array_chunk3:

$collection = Mage::getModel('catalog/product')->getCollection()->setPageSize(20)->load();
$collArray = $collection->toArray();

$cols       = 5; //set this to the # columns you want
$count      = count($collArray);//number of total records
$rowLength  = ceil($count/$cols);
$array      = array();

$output = array_chunk($collArray, $rowLength);

    @array_walk($output,function($val) use ($i){
        echo $val[$i]['sku'] . ' '; //echoing sku in the array 
    echo PHP_EOL;

In this case I'm echoing only the sku out, but instead of issuing an echo you could instead populate an output array or even a Varien_Data_Collection so you can make use of this later.

I hope that helps! Best of luck.

  1. Errors are being suppressed because we are trying to access a nonexistent key (in the case of blank cells)
  2. The use of array_walk in these examples is trivial and could be replaced with a loop. However, if you were doing something more in-depth or you had a block method that already handled how to compile rows from column values, this would be an appropriate solution.
  3. Again, errors are suppressed here. You could instead of casting to an array attempt to retrieve it via collection ID. Or, better yet, guard the condition by testing if it isset before echoing.
  • @marius yeah the content is questionable, but the image really sells it.
    – philwinkle
    Commented Mar 11, 2014 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.