Skip to content

Instantly share code, notes, and snippets.

@NickBeeuwsaert
NickBeeuwsaert / json_pretty_encode.php
Last active October 27, 2021 14:08
PHP versions prior to 5.4 didn't have JSON_PRETTY_PRINT for json_encode, so here, I guess...
<?php
//PHP got the JSON_PRETTY_PRINT option for json_encode in 5.4, and I had to use a older version of PHP,
// So I made this
function json_pretty_encode($arr, $indent=" ", $characters=array(", ", ": "), $depth=0, $eol=PHP_EOL){
//You'd think that with the plethora of functions PHP has for array operations, they'd have one to check
// if an array is associative or not
// Now, I know what your saying "Oh, but, ALL arrays in PHP are associative!" Suck it, you know what I mean
$is_assoc = (array_keys($arr) !== range(0,count($arr)-1));
end($arr);