2011-12-08 16 views
1

私は配列の配列を持っており、テーブルのデータレイアウトを作成したいと思います。これは正確なコードではなく、生成されたクラスタがどのようにクラスター化されているか(COBOLインタラクションから来ています)ですが、誰かがこれをコード化するきれいな方法を考えることができれば十分です。配列の配列を使ったデータテーブルの構築

array(
    Array(847.0, 97010, 11) 
    Array(847.0, 97012, 10) 
    Array(847.1, 97010, 08) 
    Array(847.1, 97012, 14) 
) 

は、だから私は、配列の最初の2つの要素は、常に2つの軸と第三表の内容になります

  97010 97012 
    847.0 11  10 
    847.1 08  14 

のようになります。表にこれらを配置します。

何か提案がありますか?ありがとう!

答えて

1
$table = array(); 
$columns = array(); 

// copy the array (x, y, value) into a table 
// keeping track of the unique column names as we go 
foreach ($dataSet as $point) { 
    // provided sample data used floats, ensure it is a string 
    $x = strval($point[0]); 
    $y = strval($point[1]); 
    $data = $point[2]; 

    if (!isset($table[$x])) { 
     $table[$x] = array(); 
    } 

    $table[$x][$y] = $data; 
    // quick and dirty style 'unique on insert' 
    $columns[$y] = true; 
} 

// switch the column names from title => true to just titles 
$columns = array_flip($columns); 

// Display the table 
echo '<table>'; 

// Header row 
echo '<tr>'; 
echo '<th>&nbsp;</th>'; 
foreach ($columns as $columnTitle) { 
    echo '<th>' . $columnTitle . '</th>'; 
} 
echo '</tr>'; 

// Bulk of the table 
foreach ($table as $rowTitle => $row) { 
    echo '<tr>'; 
    echo '<th>' . $rowTitle . '</th>'; 
    foreach ($columns as $columnTitle => $junk) { 
     if (isset($row[$columnTitle]) { 
      echo '<td>' . $row[$columnTitle] . '</td>'; 
     } else { 
      // Handle sparse tables gracefully. 
      echo '<td>&nbsp;</td>'; 
     } 
    } 
    echo '</tr>'; 
} 
echo '</table>'; 
0

私が理解し何から:

$arr[847.0][97010] = '11'; 
$arr[847.0][97012] = '10'; 
$arr[847.1][97010] = '08'; 
$arr[847.1][97012] = '14'; 

そして、あなたは、テーブルを作成することがあります。

$result = "<table>"; 
foreach($arr as $row_key => $row) { 
    $result .= "<tr>"; 
    foreach($row as $column_key => $data) { 
     $result .= "<td>".$data."</td>"; 
    } 
    $result .= "</tr>"; 
} 
$result .= "</table>"; 
echo $result; 
関連する問題