gpt4 book ai didi

php - 如何在特定列上动态打印列子标题?

转载 作者:行者123 更新时间:2023-11-30 23:15:02 26 4
gpt4 key购买 nike

我想在某些特定列上打印列子标题.. 如何动态打印它们?

类似的东西..

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);

关于php - 如何在特定列上动态打印列子标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18308124/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com