简体   繁体   English

在html表中显示数组

[英]Display an array in html table

I have this array: 我有这个数组:

Array
(
    [page] => Array
        (
            [0] => add
            [1] => edit
            [2] => delete
            [3] => search
        )

    [category] => Array
        (
            [0] => add
            [1] => edit
            [2] => export
        )
   )

And I want it to be displayed as a html table like this: 我希望将其显示为html表,如下所示:

Page - Category
add - add
edit - edit
delete - export
search
search

I tried in many ways but didn't work, any solutions? 我以多种方式尝试过,但没有任何解决方案?

Assuming this is PHP and that the alignment is simply based on the index of the array: 假设这是PHP,并且对齐方式仅基于数组的索引:

<?php
$var['page'] = array('add', 'edit', 'delete', 'search');
$var['category'] = array('add', 'edit', 'export');

$pages = count($var['page']);
$categories = count($var['categories']);
$max = ($pages > $categories ? $pages : $categories);

echo '<table>';
for ($i = 0; $i < $max; $i++)
{
    echo '<tr>';
    echo "<td>{$var['page'][$i]}</td>";
    echo "<td>{$var['category'][$i]}</td>";
    echo '</tr>';
}
echo '</table>';
?>

This assumes that there are more pages than categories, and that they are held in $Array['pages'] and $Array['categories'] variables: 假设页面比类别更多,并且它们保存在$ Array ['pages']和$ Array ['categories']变量中:

print '<table><tr><td>Page</td><td>Category</td></tr>';
for ($i=0; $i< sizeof($Array['pages']; $i++)
{
 print '<tr><td>';
 print $Array['pages'][$i];
 print '</td><td>';
 if ($i < sizeof($Array['categories']))
    print $Array['categories'][$i];
 print '</td></tr>';
}
print '</table>';

There are more elegant ways, but this should work. 还有更多优雅的方法,但这应该可行。

sorry i didn't tag as PHP... here is the code of my last try 抱歉,我没有标记为PHP ...这是我上次尝试的代码

echo "<table>";    
for($i=0;$i<count($array);$i++)
{
    echo "<tr>";
    foreach($array as $key=>$value)
    {
        echo "<td>".$array[$key][$i]."</td>";
    }
    echo "</tr>";
}
echo "</table>";

but anyway, JYelton answer worked pretty well, thank you =D 但是无论如何,JYelton的答案很好用,谢谢= D

I have experience getting a result from a database which is an array. 我有从数组数据库中获取结果的经验。 I made a class as a container for the uniform result set which are multiple rows. 我将一个类作为多行统一结果集的容器。 Just get the property with getter or get access it directly if public. 只需使用getter获取该属性,或者在公共的情况下直接访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM