Skip to content

Instantly share code, notes, and snippets.

@caendesilva
Created April 15, 2024 14:41
Show Gist options
  • Save caendesilva/4d17cfa6b23b221f7ad52b01332b20dd to your computer and use it in GitHub Desktop.
Save caendesilva/4d17cfa6b23b221f7ad52b01332b20dd to your computer and use it in GitHub Desktop.
<?php
function printSerializedArray(array $data): string
{
$string = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
// Convert JSON to PHP array syntax
$string = str_replace(['{', '}'], ['[', ']'], $string);
$string = str_replace('": "', "' => '", $string);
$string = str_replace('": [', "' => [", $string);
$string = str_replace('"', "'", $string);
$string = str_replace("'\n", "',\n", $string);
$string = str_replace(']', '],', $string);
$string = str_replace(',,', ',', $string);
return rtrim($string, ',');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment