陣列開始是這樣的:重排陣
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_slice
和array_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>';
作爲一個提示:如果你不'回聲'HTML,那麼掌握代碼的結構要容易得多。這樣做,例如:'<?php foreach($ headers as $ header):?>
謝謝菲利克斯,我通常會這樣做。然而,這被封裝在一個函數中,我覺得它更清晰地看到我輸出的是什麼。 – bradenkeith 2010-04-06 13:18:31