将查询拆分为数组

时间:2015-02-16 20:59:45

标签: php mysql

我有1个sql查询返回10行,我想将该查询拆分为3个数组,如下所示:

query -> (row1, row2, row3, row4, row5, row6, row7, row8, row9, row10)

colums[1] -> (row1, row4, row7, row10)   
colums[2] -> (row2, row5, row8)   
colums[3] -> (row3, row6, row9)

1 个答案:

答案 0 :(得分:0)

像这样简单的事情可以解决这个问题吗?

<?php

$a = array('row1','row2','row3','row4','row5','row6','row7','row8','row9','row10');
$b = array();
$max_num = 3;

$x = 0;
foreach($a as $value) {
    $b[$x][] = $value;
    $x++;

    if($x >= $max_num) {
        $x=0;
    }
}

var_dump($b);

array (size=3)
  0 => 
    array (size=4)
      0 => string 'row1' (length=4)
      1 => string 'row4' (length=4)
      2 => string 'row7' (length=4)
      3 => string 'row10' (length=5)
  1 => 
    array (size=3)
      0 => string 'row2' (length=4)
      1 => string 'row5' (length=4)
      2 => string 'row8' (length=4)
  2 => 
    array (size=3)
      0 => string 'row3' (length=4)
      1 => string 'row6' (length=4)
      2 => string 'row9' (length=4)