Try the following:
printf("%-40s", "Test");
40 tells printf to fill in the string so that it occupies 40 characters (this is a pad specifier). - indicates the right button (this is the alignment specifier).
See documentation for conversion specifications .
So, to print the whole array:
$max_key_length = max(array_map('strlen', array_keys($array))); $max_value_length = max(array_map('strlen', $array)); foreach($array as $key => $value) { printf("%-{$max_key_length}s %{$max_value_length}s\n", $key, $value); }
Try it here: http://codepad.org/ZVDk52ad
source share