我有一些数据,例如:
Array
(
[0] => Array
(
[a] => largeeeerrrrr
[b] => 0
[c] => 47
[d] => 0
)
[1] => Array
(
[a] => bla
[b] => 1
[c] => 0
[d] => 0
)
[2] => Array
(
[a] => bla3
[b] => 0
[c] => 0
[d] => 0
)
)
我想产生如下输出:
title1 | title2 | title3 | title4
largeeeerrrrr | 0 | 47 | 0
bla | 1 | 0 | 0
bla3 | 0 | 0 | 0
在 PHP 中实现此目的最简单的方法是什么?我想避免使用库来完成如此简单的任务。
use printf http://php.net/manual/en/function.sprintf.php
$i=0;
foreach( $itemlist as $items)
{
foreach ($items as $key => $value)
{
if ($i++==0) // print header
{
printf("[%010s]|", $key );
echo "\n";
}
printf("[%010s]|", $value);
}
echo "\n"; // don't forget the newline at the end of the row!
}
这使用 10 个填充空间。正如 BoltClock 所说,您可能需要首先检查最长字符串的长度,否则您的桌子将被长字符串顶住。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)