Created
July 21, 2019 17:08
-
-
Save tomkrush/a281ade6b22260315ae4bfb8cf520bff to your computer and use it in GitHub Desktop.
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 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