创建一个表,在PHP中的子查询的值 [英] Creating a table with values of a subquery in PHP

查看:196
本文介绍了创建一个表,在PHP中的子查询的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我亲爱而忠实的堆垛机!我有以下数组:

Hello my dear and faithful 'stackers'! I have following array:

$array = array(
    "0" => array("1", "2", "3", "4", "5"),
    "1" => array("I", "II", "III", "IV", "V"),
    "2" => array("i", "ii", "iii", "iv", "v"),
    "3" => array("A", "B", "C", "D", "E"),
    ...,
    "9" => array("a", "b", "c", "d", "f"),
);

和想创建一个表与子阵的就是这样的值:

and would like to create a table with the values of the subarrays just like that:

| 1 | 2  |  3  | 4  | 5 |
| I | II | III | IV | V |
| i | ii | iii | iv | v |
| A | B  |  C  | D  | E |
|...|    |     |    |   |
| a | b  |  c  | d  | e |

我试过这样:

$table = '<table border=1>';
// Select values of the array to create table.
foreach($array as $row){
    foreach($row as $key => $value){
        $table .= '
            <tr>
                <td>'.$row['0'].'</td>
                <td>'.$row['1'].'</td>
                <td>'.$row['2'].'</td>
                <td>'.$row['3'].'</td>
                <td>'.$row['4'].'</td>
            </tr>';
    }
}
$table .= '</table>';

,但问题是,每个子阵列在表中显示的8倍...

but the problem is that every subarray is displayed 8 times in the table...

| 1 | 2  |  3  | 4  | 5 |
| 1 | 2  |  3  | 4  | 5 |
| 1 | 2  |  3  | 4  | 5 |
| 1 | 2  |  3  | 4  | 5 |
| 1 | 2  |  3  | 4  | 5 |
| 1 | 2  |  3  | 4  | 5 |
| 1 | 2  |  3  | 4  | 5 |
| 1 | 2  |  3  | 4  | 5 |
| I | II | III | IV | V |
| I | II | III | IV | V |
| I | II | III | IV | V |
| I | II | III | IV | V |
| I | II | III | IV | V |
| I | II | III | IV | V |
| I | II | III | IV | V |
| I | II | III | IV | V |
|...|    |     |    |   |
| a | b  |  c  | d  | e |
| a | b  |  c  | d  | e |
| a | b  |  c  | d  | e |
| a | b  |  c  | d  | e |
| a | b  |  c  | d  | e |
| a | b  |  c  | d  | e |
| a | b  |  c  | d  | e |
| a | b  |  c  | d  | e |

我不知道我做错了,会一个preciate你的帮助。在此先感谢!

I don't know what I'm doing wrong and would apreciate your help. Thanks in advance!

== ==更新

在从费利克斯·加侬 - 格雷尼尔某种提示code必须是:

After some kind tips from Félix Gagnon-Grenier the code must be:

<?php

$array = array(
"0" => array("1", "2", "3", "4", "5"),
"1" => array("I", "II", "III", "IV", "V"),
"2" => array("i", "ii", "iii", "iv", "v"),
"3" => array("A", "B", "C", "D", "E"),
"9" => array("a", "b", "c", "d", "f")
);

print_r($array);


$table = '<table border=1>';
// Select values of the array to create table.
foreach($array as $key => $row){
    $table .= '<tr>';
    foreach($row as $value){
        $table .= '
                <td>'.$value.'</td>
        ';
    }
    $table .= '</tr>';
}
$table .= '</table>';

echo $table;
?>

所以它工作正常!

So it works fine!

推荐答案

你几乎得到了它。

foreach($array as $key => $row){
    $table.= '<tr>';
    foreach($row as $value){
        $table.= '<td>'.$value.'</td>';
    }
    $table.= '</tr>';
}

您想为每个初始数组的新表行,然后为每个值的新表单元格

you want a new table row for each initial array, then a new table cell for each value

这篇关于创建一个表,在PHP中的子查询的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆