下面的代碼測試,以查看值如何某些針對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循環來實現?
此代碼工作與否? –