Skip to content

Instantly share code, notes, and snippets.

@tomkrush
Created July 21, 2019 17:08
Show Gist options
  • Save tomkrush/a281ade6b22260315ae4bfb8cf520bff to your computer and use it in GitHub Desktop.
Save tomkrush/a281ade6b22260315ae4bfb8cf520bff to your computer and use it in GitHub Desktop.
<?php
function formatJSONToPHP($value) {
$output = [];
if (is_array($value) === false) {
if (is_numeric($value)) {
$output[] = sprintf("%s,", $value);
} else {
$output[] = sprintf("'%s',", $value);
}
}
if (is_array($value)) {
$output[] = "[";
$array = [];
foreach($value as $subKey => $subValue) {
$subValue = formatJSONToPHP($subValue);
$subValue = explode("\n", $subValue);
$subValue = array_map(function($item) {
return ' ' . $item;
}, $subValue);
$subValue = implode("\n", $subValue);
if (is_numeric($subKey)) {
$array[] = sprintf("%s", $subValue);
} else {
$array[] = sprintf("'%s' => %s", $subKey, $subValue);
}
}
foreach($array as $item) {
$output[] = ' ' . $item;
}
$output[] = "],";
}
return implode("\n", $output);
}
print_r(formatJSONToPHP(json_decode($json, true)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment