diff --git a/src/MD_STD.php b/src/MD_STD.php index 1a98fc6..9335779 100644 --- a/src/MD_STD.php +++ b/src/MD_STD.php @@ -1087,4 +1087,44 @@ final class MD_STD { return $lowest_option; } + + /** + * Prints an array JSON-encoded. Does so entry by entry to reduce memory consumption. + * + * @param array $input Input. + * @param boolean $html_encode HTML encodes. + * + * @return void + */ + public static function print_json_encoded(array &$input, bool $html_encode):void { + + $last = count($input); + if (array_is_list($input)) { + + echo '['; + $i = 0; + foreach ($input as $value) { + if ($html_encode === true) echo htmlspecialchars(json_encode($value)); + else echo json_encode($value); + $i++; + if ($i !== $last) echo ','; + } + echo ']'; + + } + else { + + echo '{'; + $i = 0; + foreach ($input as $key => $value) { + if ($html_encode === true) echo htmlspecialchars('"' . $key . '" : ' . json_encode($value)); + else echo '"' . $key . '": ' . json_encode($value); + $i++; + if ($i !== $last) echo ','; + } + echo '}'; + + } + + } }