foreach从数组中的数据构建HTML表

时间:2014-04-04 01:42:52

标签: php html arrays

以下代码测试以查看某些值如何针对PHP函数:

//array to hold test values
$values = array
(
    'empty string' => '',
    'space' => ' ',
    'false' => false,
    'true' => true,
    'empty array' => array(),
    'null' => null,
    '0 as string' => '0',
    '0 as integer' => 0,
    '0 as float' => 0.0,
    '$var declared but without a value' => $var
);

foreach($values as $key => $value):
    $result['isset'][$key] = isset($value);
    $result['empty'][$key] = empty($value);
    $result['is_null'][$key] = is_null($value);
    $result['is_numeric'][$key] = is_numeric($value);   //added this line on 10/7/13
    $result['strlen'][$key] = strlen($value);   //added this line on 12/23/13
endforeach;

//view results by function
echo "<h3>view results by function</h3><pre>" . print_r($result,1) . "</pre>";

此代码输出的数组如下:

Array
(
    [isset] => Array
        (
            [empty string] => 1
            [space] => 1
            [false] => 1
            [true] => 1
            [empty array] => 1
            [null] => 
            [0 as string] => 1
            [0 as integer] => 1
            [0 as float] => 1
            [$var declared but without a value] => 
        )

    [empty] => Array
        (
            [empty string] => 1
            [space] => 
            [false] => 1
            [true] => 
            [empty array] => 1
            [null] => 1
            [0 as string] => 1
            [0 as integer] => 1
            [0 as float] => 1
            [$var declared but without a value] => 1
        )

    [is_null] => Array
        (
            [empty string] => 
            [space] => 
            [false] => 
            [true] => 
            [empty array] => 
            [null] => 1
            [0 as string] => 
            [0 as integer] => 
            [0 as float] => 
            [$var declared but without a value] => 1
        )

    [is_numeric] => Array
        (
            [empty string] => 
            [space] => 
            [false] => 
            [true] => 
            [empty array] => 
            [null] => 
            [0 as string] => 1
            [0 as integer] => 1
            [0 as float] => 1
            [$var declared but without a value] => 
        )

    [strlen] => Array
        (
            [empty string] => 0
            [space] => 1
            [false] => 0
            [true] => 1
            [empty array] => 
            [null] => 0
            [0 as string] => 1
            [0 as integer] => 1
            [0 as float] => 1
            [$var declared but without a value] => 0
        )

)

在HTML表格中查看结果会更友好。到目前为止,我只是成功构建了包含标签的行:

$html = "\n<table>\n<tr>\n\t<th>test</th>\n";
//table header row
foreach ($result as $key => $value):
    $html .= "\t<th>$key</th>\n";
endforeach;
$html .= "</tr>\n";
//add detail rows here
$html .= "</table>\n";

我很难弄清楚如何正确构建细节行。目标是每行显示一个测试名称()。测试是$ values数组中的键。 s将是测试名称,回答isset,回答为空,回答is_null,回答is_numeric,回答strlen。如何使用foreach循环实现这一目标?

4 个答案:

答案 0 :(得分:1)

在代码末尾添加以下行:

print("<table border=1>
<tr>
    <th></th>
    <th>isset</th>
    <th>empty</th>
    <th>is_null</th>
    <th>is_numeric</th>
    <th>strlen</th>
</tr>".PHP_EOL);
foreach($values as $key => $value):
print("<tr>
    <td>$key</td>
    <td>{$result["isset"][$key]}</td>
    <td>{$result["empty"][$key]}</td>
    <td>{$result["is_null"][$key]}</td>
    <td>{$result["is_numeric"][$key]}</td>
    <td>{$result["strlen"][$key]}</td>
</tr>".PHP_EOL);
endforeach;
print("</table>");

答案 1 :(得分:1)

我认为如果重新排列阵列,这更容易实现。这就是我的所作所为。

$values = array
(
    'empty string' => '',
    'space' => ' ',
    'false' => false,
    'true' => true,
    'empty array' => array(),
    'null' => NULL,
    '0 as string' => '0',
    '0 as integer' => 0,
    '0 as float' => 0.0,
    '$var declared but without a value' => @$var
);
$tests = array('isset','empty','is_null','is_numeric','strlen');
?>
<table>
    <tr>
        <th>test</th>
        <th><?=implode("</th>\n<th>",$tests)?></th>
    </tr>
<?php foreach($values as $key=>$value): ?>
    <tr>
        <td><?=$key?></td>  
        <?php foreach($tests as $test): ?>
        <td>
            <?php if($test == 'isset'): ?>
                <?=(int)isset($value)?>
            <?php elseif($test == 'empty'): ?>
                <?=(int)empty($value)?>
            <?php else : ?>
                <?=@(int)$test($value)?>
            <?php endif; ?>
            </td>
        <?php endforeach; ?>
    </tr>
<?php endforeach; ?>
</table>

现在,如果您想轻松地使用测试数组,则可以添加更多测试,并且除了在内爆之外,您不需要使用\n\t来设置标记格式。 @符号用于抑制代码在2个位置生成的警告 - 一个在使用未定义的变量时为2,在尝试在数组上使用strlen时。

内部foreach中条件的原因是issetempty是语言结构而不是函数,因此无法动态调用它们。

我知道这并不是你所要求的,但我学到了一些东西,所以我想分享。

答案 2 :(得分:0)

这是完整的代码。我认为它应该适合你。

<?php
$var="";
$values = array
(
    'empty string' => '',
    'space' => ' ',
    'false' => false,
    'true' => true,
    'empty array' => array(),
    'null' => null,
    '0 as string' => '0',
    '0 as integer' => 0,
    '0 as float' => 0.0,
    '$var declared but without a value' => $var
);

foreach($values as $key => $value)
{
    $result['isset'][$key] = isset($value);
    $result['empty'][$key] = empty($value);
    $result['is_null'][$key] = is_null($value);
    $result['is_numeric'][$key] = is_numeric($value);   //added this line on 10/7/13
    if(!(is_array($value)))
    $result['strlen'][$key] = strlen($value);   //added this line on 12/23/13 

}
//echo "<h3>view results by function</h3><pre>" . print_r($result,1) . "</pre>";
?>
<table border="1">
    <tr>
        <th>Key</th>
        <th>Value</th>   
    </tr>
<?php
foreach ($result as $key => $value):

    foreach ($value as $k => $val) 
    {
?>
        <tr><td><?php echo $k ?></td><td><?php echo $val ?></td></tr>
<?php 
    }
endforeach;
?>

答案 3 :(得分:0)

我建议你&#34;旋转&#34;你的数组结构首先。来自&#34;结果 - &gt;列 - &gt;行&#34;到&#34;结果 - &gt;行 - &gt;列&#34;

foreach($values as $key => $value):
    $result[$key]['isset'] = (int)isset($value);
    $result[$key]['empty'] = (int)empty($value);
    $result[$key]['is_null'] = (int)is_null($value);
    $result[$key]['is_numeric'] = (int)is_numeric($value);
    $result[$key]['strlen'] = (int)strlen($value);
endforeach;


$html = "<table>\n";

$html .= "<tr>";
$first_row = reset($result);
foreach ($first_row as $key => $value):
    $html .= "<th>$key</th>";
endforeach;
$html .= "</tr>\n"

foreach ($result as $row):
    $html .= "<tr>";
    foreach($row as $key => $value):
        $html .= "<td>$value</td>";
    endforeach;
    $html .= "</tr>\n";
endforeach;

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