2014-04-04 19 views
0

下面的代碼測試,以查看值如何某些針對PHP函數出來:的foreach建立從數據的HTML表以陣列

//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表格中查看結果將更加友好。到目前爲止,我只有成功構建包含th標籤的行:

$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循環來實現?

回答

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>"); 
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; 
?> 
+0

此代碼工作與否? –

0

我建議你到「旋轉」的陣列結構第一左右。從「結果 - >列 - >行」到「結果 - >行 - >列」

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" 
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 : ?> 
       <[email protected](int)$test($value)?> 
      <?php endif; ?> 
      </td> 
     <?php endforeach; ?> 
    </tr> 
<?php endforeach; ?> 
</table> 

現在,如果你想測試陣列,您可以添加更多的測試容易,你不需要使用\n\t格式化標記除了在破滅。 @符號用於抑制代碼在2位產生的警告 - 當使用未定義的變量時爲1,而當嘗試在陣列上使用strlen時爲2。

內部條件的原因是因爲issetempty是語言結構而不是函數,所以它們不能被動態調用。

我知道這不是你要求的,但是我學到了一些東西,所以我會分享。