Last active
May 13, 2020 10:26
-
-
Save zecka/e23f32c98ac0eda32a769dd117ceef18 to your computer and use it in GitHub Desktop.
Php print an array as php code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 .= ' '; | |
} | |
for ($i = 1; $i < $deep; $i++) { | |
$indent_close .= ' '; | |
} | |
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