I have some data like:
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
)
)
And I want to produce an output like:
title1 | title2 | title3 | title4
largeeeerrrrr | 0 | 47 | 0
bla | 1 | 0 | 0
bla3 | 0 | 0 | 0
Which is the simples way to achieve this in PHP? I'd like to avoid using a library for such simple task.
use printf
$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!
}
This uses 10 padding spaces. As BoltClock says, you probably want to check the length of the longest string first or your table will be jacked up on long strings.