2013-07-23 17 views
6

正在檢查其他用於對列表值進行排序的方法垂直用於表格中,但其中大部分相當於將表格向右翻轉90度。我一直在想辦法正確地實現這一點,但我認爲我需要一些幫助。在保持表格結構的同時垂直顯示錶格值

例如,表(水平排列):

a b c d e 
f g h i j 
k l m n o 
p q r 

被重新排序,以(垂直排列):

a e i m p 
b f j n q 
c g k o r 
d h l 

正如你可以看到自上次2個細胞是結構被保留空。

不喜歡這樣的:

a e i m q 
b f j n r 
c g k o 
d h l p 

在這個例子中,該表是類似於側向翻轉它。有誰知道如何正確地做到這一點?

+0

你想從數據庫中呈現表中的值? –

+0

是的,但是你可以保持簡單並且在這個例子中使用一個數組:'array('a','b','c','d','e'...)'。 – enchance

回答

2

編輯:這比我想象的更難,我第一次(或兩個)搞砸了。它現在應該工作。

比方說,你已經存儲在一個二維數組你的表結構:

$data = array(
    array('a', 'b', 'c', 'd', 'e'), 
    array('f', 'g', 'h', 'i', 'j'), 
    array('k', 'l', 'm', 'n', 'o'), 
    array('p', 'q', 'r') 
); 

既然你想保持相同的「形」,你需要確定表的尺寸。要做到這一點,我們可以取第一行的count,因爲我們知道第一行必須是表的最大寬度。高度只是數組中元素的數量。

$width = count($data[0]); // 5 
$height = count($data); // 4 

我們也需要元素的總數,但我們可以高估$ width * $ height。

$total = $width * $height; // 20 

然後,它真的只是一個小數學來計算事情的發展。我們必須爲舊的和新的指數使用單獨的計數器,因爲一旦我們開始出現漏洞,我們將不得不以不同的方式增加它們。

$new_data = array(); 
$j = 0; 
for($i = 0; $i < $total; $i++) { 
    $old_x = floor($i/$width); // integer division 
    $old_y = $i % $width;  // modulo 

    do { 
    $new_x = $j % $height;  // modulo 
    $new_y = floor($j/$height); // integer division 
    $j++; 
    // move on to the next position if we have reached an index that isn't available in the old data structure 
    } while (!isset($data[$new_x][$new_y]) && $j < $total); 

    if (!isset($new_data[$new_x])) { 
    $new_data[$new_x] = array(); 
    } 
    if (isset($data[$old_x][$old_y])) { 
    $new_data[$new_x][$new_y] = $data[$old_x][$old_y]; 
    } 
} 
+0

糟糕,我在第一次發佈它之前沒有對它進行測試,並且我改變了寬度和高度。該帖子現在已修復。 – seanmk

+0

我試過了,它不起作用。當你的第一行應該是'a i m p'時,你的第一行就會成爲'a i m q''。由於第一行是錯誤的,所以其餘行也會移動。另外,行1-3需要有5列,而在你的例子中只有1-2行有5個元素。 – enchance

+0

好吧,我想了解更多,並更新了答案。它現在應該工作。 – seanmk

1

訣竅是爲「懸掛」列減去一個。那是最後一行缺少值的列。

// setup some initial test data... rip this out and replace with whatever 
$values = array(); 
for ($x = 0; $x < 18; ++$x) 
    $values[] = chr($x + ord("a")); 

我們可以任意改變列數。行數取決於我們數據的大小除以我們使用的列數。

// change # of columns to desired value 
$columns = 5; 
$rows = (int) ceil(count($values)/$columns); 

某些列掛起。這是該列在最後一行中缺少一個值。

// the number of columns that will "hang" or miss a value in the last row 
$hanging_columns = $columns * $rows - count($values); 

$counter = 0; 
for ($y = 0; $y < $rows; ++$y) { 
    for ($x = 0; $x < $columns; ++$x) { 
    // if we've displayed all values, stop 
    if ($counter++ >= count($values)) break; 

    // calculate the correct index to display 
    $index = ($y + $x * $rows); 
    // if we are in a hanging column, we need to back up by one 
    if ($x > $columns - $hanging_columns) $index -= 1; 

    // display the value 
    echo $values[$index] . " "; 
    } 
    echo "\n"; 
} 
0

這裏的另一個解決方案

$unflipped = array('a', 'b', 'c', 'd', 'e', 
       'f', 'g', 'h', 'i', 'j', 
       'k', 'l', 'm', 'n'); 


function flip90($arr, $lineHeight) 
{ 
    $tbl = array(); 

    $index = 0; 
    $counter = 1; 


    for($i = 0; $i < count($arr); $i++) 
    { 
     if($counter > $lineHeight) 
     { 
      $index++; 
      $counter = 1; 
     } 

     if($counter <= $lineHeight) 
     { 
      $tbl[$index][$counter] = $arr[$i]; 

      $counter++; 
     } 

    } 

    return $tbl; 
} 

$flipped = flip90($unflipped, 5); 
echo "<pre>"; 
var_dump($flipped); 
echo "<pre>"; 

功能需要陣列以及測量表格中的行高

相關問題