Skip to content

Instantly share code, notes, and snippets.

@zecka
Last active May 13, 2020 10:26
Show Gist options
  • Save zecka/e23f32c98ac0eda32a769dd117ceef18 to your computer and use it in GitHub Desktop.
Save zecka/e23f32c98ac0eda32a769dd117ceef18 to your computer and use it in GitHub Desktop.
Php print an array as php code
<?php
function bo_print_nice_array($array)
{
echo '$array=';
bo_print_nice_array_content($array, 1);
echo ';';
}
function bo_print_nice_array_content($array, $deep = 1)
{
$indent = '';
$indent_close = '';
echo "[";
for ($i = 0; $i < $deep; $i++) {
$indent .= '&nbsp;&nbsp;&nbsp;&nbsp;';
}
for ($i = 1; $i < $deep; $i++) {
$indent_close .= '&nbsp;&nbsp;&nbsp;&nbsp;';
}
foreach ($array as $key => $value) {
echo "<br>" . $indent;
echo '"' . $key . '" => ';
if (is_string($value)) {
echo '"' . $value . '"';
} elseif (is_array($value)) {
bo_print_nice_array_content($value, ($deep + 1));
}elseif(is_numeric($value)) {
echo $value;
} elseif(is_object($value)) {
echo "unable to format type:".gettype($value);
}
echo ',';
}
echo '<br>' . $indent_close . ']';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment