重新排列数组

时间:2010-04-06 13:05:48

标签: php arrays

数组的开头如下:

Array ( 
  [SMART Board] => Array ( [0] => sb1 [1] => sb2 [2] => sb3 ) 
  [Projector] => Array ( [0] => pr1 [1] => pr2 [2] => pr3 ) 
  [Speakers] => Array ( [0] => sp1 [1] => sp2 [2] => sp3 ) 
  [Splitter] => Array ( [0] => spl1 [1] => spl2 [2] => spl3 ) 
  [Wireless Slate] => Array ( [0] => ws1 [1] => ws2 [2] => ws3 ) 
)

键用作我的表列标题。它们各自的数组用于携带列信息。

我已将array_slicearray_merge_recursive进一步拆分为2个数组 - 1个包含列名,另一个包含:

Array ( 
  [0] => sb1  
  [1] => sb2  
  [2] => sb3  
  [3] => pr1  
  [4] => pr2  
  [5] => pr3  
  [6] => sp1  
  [7] => sp2  
  [8] => sp3  
  [9] => spl1  
  [10] => spl2  
  [11] => spl3  
  [12] => ws1  
  [13] => ws2  
  [14] => ws3  
)

然而,当我尝试写表时,我得到键0,1,2作为列数据,然后行中断然后3,4,5然后行中断......等等。

我需要它是0,3,6,9,12排休息1,4,7,10,13排休息等......

如何重新排列我的数组以允许这样做,或者重写数据如何进入表格以便正确的信息与适当的列对齐?

foreach(unserialize($dl->data) as $data){

    //first 3 are specialinstructions, system, and room
    $uniques = array_slice($data,0,3);

    $rows = array_slice($data,3);
    $rows2 = array_merge_recursive($rows2, $rows);

    //get the specialinstructions, system, and room
    foreach($uniques as $unique){
        echo $unique."<br/>";
    }///foreach uniques

    echo "<br>";

}///foreach unserialized

$numberofrooms = count($rows2[key($rows2)]);
$numberofproducts = count($rows2);
print_r($rows2);

unset($rows);

//write the individual rows
foreach($rows2 as $header=>$rowset){

    $headers[] = $header;

    foreach($rowset as $row){

        $rows[] = $row;

    }//foreach rowset

}//foreach rows2

echo "<p>";
print_r($rows);

echo '<table class="data-table">
          <caption>DL</caption>

          <thead><tr>';
foreach($headers as $header){
    echo "<th>".$header."</th>";
}
echo '</tr></thead>';
echo '<tbody>';
$i = 0;
foreach($rows as $row){
    if($i == 3 || $i == 0){
        echo "<tr>";
        $i = 1;
    }
    echo '<td>'.$row.'</td>';
    if($i == 2){
        echo "</tr>";
    }
    $i++;
}
echo '</tbody></table>';

1 个答案:

答案 0 :(得分:1)

您可以使用原始数组和以下代码来显示您想要的方式:

$i=0;
while($i<3){ // 3 is the number of items in each array
    foreach($originalArray as $arr){
        print($arr[$i].'<br/>');
    }
    print('<hr/>');
    $i++;
}

未经测试但你应该得到漂移。