我想在某些特定列上打印列子标题..如何动态打印它们?
类似的东西..
col heading 1 col heading 2 col heading 3
------------- ------------- -------------------------
col1 col2 col1 col2 col1 col2 col3 col4
<?php
$recievedArray = array(array("abc def", "abc ghi", "abc jkl"),
array("123 456", "123 789")); //Consider you getting the result from db to php as array
/**
*
* @param array $array Two dimentional array to format
* @return string
*/
function formatter(Array $array)
{
foreach ($array as $key => $value) {
$sub = '';
foreach ($value as $subKey => $subValue) {
$sub .= strstr($subValue, ' '); //Get string after first space
$exploded[strstr($subValue, ' ', TRUE)] = $sub; //Get string before first space
}
}
$html = '';
$tableStart = '<table>';
$tabeEnd = '</table>';
$th = '';
$td = '';
$lineTd = '';
$trStart = '<tr>';
$trEnd = '</tr>';
//Here th and td are prepared
foreach ($exploded as $key => $value) {
$th .= '<th>' . $key . '</th>';
$lineTd .= '<td>' . str_repeat('--', strlen($value)) . '</td>';
$td .= '<td>' . $value . '</td>';
}
//Concatenate the string to get the formated table data
$html .= $tableStart .
$trStart .
$th .
$trEnd .
$trStart .
$lineTd .
$trEnd .
$trStart .
$td .
$trEnd .
$tabeEnd;
return $html;
}
echo formatter($recievedArray);