-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathascii-table.php
More file actions
46 lines (35 loc) · 1.08 KB
/
ascii-table.php
File metadata and controls
46 lines (35 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
function ascii_table($data) {
$data = (array)$data;
$keys = array_keys(end($data));
# calculate optimal width
$wid = array_map('strlen', $keys);
foreach($data as $row) {
foreach(array_values($row) as $k => $v)
$wid[$k] = max($wid[$k], strlen($v));
}
# build format and separator strings
foreach($wid as $k => $v) {
$fmt[$k] = "%-{$v}s";
$sep[$k] = str_repeat('-', $v);
}
$fmt = '| ' . implode(' | ', $fmt) . ' |';
$sep = '+-' . implode('-+-', $sep) . '-+';
# create header
$buf = array($sep, vsprintf($fmt, $keys), $sep);
# print data
foreach($data as $row) {
$buf[] = vsprintf($fmt, $row);
$buf[] = $sep;
}
# finis
return implode("\n", $buf);
}
# example
$data = array(
array('id' => 1, 'field1' => 'foo', 'field2' => 'bar'),
array('id' => 2, 'field1' => 'lorem', 'field2' => 'ipsum'),
array('id' => 3, 'field1' => 'anton', 'field2' => 'helloooooooooooo'),
array('id' => 4, 'field1' => 'asdsadasdasdas', 'field2' => 'asd'),
);
echo ascii_table($data);