Skip to content

Instantly share code, notes, and snippets.

@SamuelDavis
Created August 5, 2018 01:59
Show Gist options
  • Save SamuelDavis/82bedf1e10083d91ee91cc344f933663 to your computer and use it in GitHub Desktop.
Save SamuelDavis/82bedf1e10083d91ee91cc344f933663 to your computer and use it in GitHub Desktop.
Pretty-print an array of objects
<?php
function format(array $data, string $type): string
{
$data = array_map(function ($obj) {
return array_map(function ($value) {
if ($value === null) {
return 'null';
}
if ($value === true) {
return 'true';
}
if ($value === false) {
return 'false';
}
return $value;
}, get_object_vars($obj));
}, array_slice($data, 0, 2));
$formatted = print_r($data, true);
return preg_replace('/\[([0-9]+)\] => Array/', "[$1] => {$type} Object", $formatted);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment