Skip to content

Instantly share code, notes, and snippets.

@thgs
Created November 17, 2015 09:08
Show Gist options
  • Save thgs/dccc89edfa2d468da3b6 to your computer and use it in GitHub Desktop.
Save thgs/dccc89edfa2d468da3b6 to your computer and use it in GitHub Desktop.
Array pretty printer for PHP
<?php
function arrToStr(array $a)
{
if (empty($a)) return '[]';
foreach($a as $k => $v)
{
if (is_array($v))
$a[$k] = arrToStr($v);
}
$elems = implode(', ', $a);
return '['.$elems.']';
}
$a = [1,2,[3,[],[[]],4]];
echo arrToStr($a); # output: [1, 2, [3, [], [[]], 4]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment