0

我在从二维数组创建表时确实有一点问题。

我从一页中获取动态数组,例如,

Array
(
 [0] => Array
    (
        [0] => 1374
        [id] => 1374 )
        [1] => 223
        [version_id] => 223

[1] => Array
    (
        [0] => 1540
        [id] => 1540)
        [1] => 224
        [version_id] => 224

[2] => Array
    (
        [0] => 1541
        [id] => 1541)
        [1] => 225
        [version_id] => 225
)

我想要表格中的数据,例如,

必需的

4

3 回答 3

1

通过html结构

“行”<tr>必须先渲染,然后是“列”<td>

按每一行渲染会更容易

更新

这正是你想要的..

第三次编辑

定义样本 $data

$data = array(
        array(
            0 => 1374,
            'id' => 1374,
            1 => 223,
            'version_id' => 223
        ),
        array(
            0 => 1540,
            'id' => 1540,
            1 => 224,
            'version_id' => 224
        ),
        array(
            0 => 1541,
            'id' => 1541,
            1 => 225,
            'version_id' => 225
        )
    );

假设您知道字段信息或通过 mysql_field_name 检索

    $fields = array('id', 'version_id'); 
    echo '<table class="spec">
        <thead>
            <tr>
                <th>name</th>';

for ($n = 0; $n < sizeof($data); $n++) {
    echo '
            <th>array_data-' . $n . '</th>';
}

echo '
            </tr>
        </thead>
        <tbody>';

for ($i = 0; $i < sizeof($fields); $i++) {
    echo '<tr>
            <td>' . $fields[$i] . '</td>';

    foreach ($data as $row) {
        echo '<td>' . $row[$i] . '</td>';
    }
    echo '</tr>';
}

echo '</tbody>
    </table>';
于 2012-04-20T07:32:26.460 回答
1
$multidim_arr = array
(
 array
    ( 
        0 => 1374,
        1 => 223
),
array
    (
        0 => 1540,
        1 => 224
),
array
    (
        0 => 1541,
        1 => 225
)
);
$n = count($multidim_arr);
$z = count($multidim_arr[$n-1]);
echo "<table>";
for($j = 0; $j<$z;$j++) {
  echo "<tr>";
  for($i = 0; $i<$n; $i++) {
    echo "<td>".$multidim_arr[$i][$j]."</td>";
  }
  echo "</tr>";
}
echo "</table>";

首先在 PDO 中做 PDO::FETCH_NUM 而不是 PDO::FETCH_BOTH 检查http://php.net/manual/en/pdostatement.fetch.php

问候

@edit 或使用键:

<?php

$multidim_arr = array
(
 array
    ( 
        'id' => 1374,
        'version_id' => 223
),
array
    (
        'id' => 1540,
        'version_id' => 224
),
array
    (
        'id' => 1541,
        'version_id' => 225
)
);
$n = count($multidim_arr);
$z = count($multidim_arr[$n-1]);
$keys = array_keys($multidim_arr[$n-1]);
echo "<table>";
for($j = 0; $j<$z;$j++) {
  echo "<tr>";
  for($i = 0; $i<$n; $i++) {
    echo "<td>key: ".$keys[$j]." and value: ".$multidim_arr[$i][$keys[$j]]."</td>";
  }
  echo "</tr>";
}
echo "</table>";

@edit 另一个例子

<?php

$multidim_arr = array
(
 array
    ( 
    0 => 1374,
        'id' => 1374,
    1 => 223,
        'version_id' => 223
),
array
    (
    0 => 1540,
        'id' => 1540,
        1 => 224,
        'version_id' => 224
),
array
    (
    0 => 1541,
        'id' => 1541,
        1 => 225,
        'version_id' => 225
)
);
$n = count($multidim_arr);
$z = count($multidim_arr[$n-1])/2;
$keys = array('id', 'version_id');
echo "<table>";
for($j = 0; $j<$z;$j++) {
  echo "<tr>";
  for($i = 0; $i<$n; $i++) {
    echo "<td>key: ".$keys[$j]." and value: ".$multidim_arr[$i][$keys[$j]]."</td>";
  }
  echo "</tr>";
}
echo "</table>";

但在这里你必须知道钥匙。

于 2012-04-20T07:40:57.623 回答
0

//尚未测试,但应该很好..

$count_multi = count($multidim_arr);
echo '<table border="1"><tbody><tr>';
for($i=0;$i<$count_multi;$i++)
{
   $subarr = $multidim_arr[$i];
   echo '<td>Content at index'.$i.' of multi-dimensional array <br />'.$subarr['id'].'</td>';
}
echo '</tr></tbody></table>';
于 2012-04-20T07:28:21.697 回答