如何在PHP中创建和显示多维数组

时间:2011-03-18 11:10:15

标签: php arrays multidimensional-array

我想在PHP中创建一个网格,并在选定单元格中显示一个值,其值与数据库单元格值匹配。

网格示例:

A1|B1|C1|D1|E1|F1|G1|H1|
A2|B2|C2|D2|E2|F2|G2|H2|
A3|B3|C3|D3|E3|F3|G3|H3|

在数据库中,我有一个名为cell_address的列,其中包含[A1,A2,D3,H3];

等值

如何将数据库值放在网格中?

3 个答案:

答案 0 :(得分:3)

试试这个:

<?php

$cell_address = "[A1,A2,D3,H3]";

$ar_columns = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H');
$ar_rows = array('1', '2', '3');

$ar_addresses = explode(",", substr($cell_address, 1, -1));

$html = "<table>
  <tr>
    <th></th>
    <th>".implode("</td><td>", $ar_columns)."</th>
  </tr>\n";

foreach($ar_rows as $row)
{
  $html .= "<tr><th>".$row."</th>\n";

  foreach($ar_columns as $col)
  {
    $cell_str = (in_array($col.$row, $ar_addresses) ? "match" : "&nbsp;");
    $html .= "<td>".$cell_str."</td>\n";
  }

  $html .= "</tr>\n";
}

$html .= "</table>\n";

echo $html;

答案 1 :(得分:0)

只需使用mysql_fetch_array()从数据库中获取数据并将其显示在网格中......

答案 2 :(得分:0)

对于这样的问题,我只能说:阅读手册(例如this,其中也包含示例)并研究语言基础知识