Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function cleanKeys($obj, $debug = false) {
- $cleanedObj = [];
- foreach ($obj as $key => $value) {
- $cleanKey = $key;
- $cleanValue = $value;
- // Reassign numeric string keys as float type
- if (is_string($cleanKey) && strval(floatval($cleanKey)) === $cleanKey) {
- $cleanKey = floatval($cleanKey);
- }
- // Reassign numeric string values as float type
- if (is_string($cleanValue) && strval(floatval($cleanValue)) === $cleanValue) {
- $cleanValue = floatval($cleanValue);
- }
- if (is_string($cleanValue)) {
- // Replace escaped slashes with regular slashes
- $cleanValue = str_replace('\/', '/', $cleanValue);
- // Remove escaped double quotes from the value
- $cleanValue = str_replace('\"', '', $cleanValue);
- }
- // Add spaces inside double curly braces if the content doesn't start or end with whitespace
- if (is_string($cleanValue)) {
- if (preg_match_all("/{{(?<inner>[^\s].+[^\s])}}/", $cleanValue, $matches, PREG_SET_ORDER) > 0) {
- foreach ($matches as $match) {
- $cleanValue = str_replace($match[0], "{{ " . strtolower($match['inner']) . " }}", $cleanValue);
- }
- }
- }
- // Assign the cleaned key and value
- $cleanedObj[$cleanKey] = $cleanValue;
- }
- // Extract keys and sort them naturally
- $keys = array_keys($cleanedObj);
- natcasesort($keys);
- // Rebuild the array with sorted keys
- $sortedObj = [];
- foreach ($keys as $key) {
- $sortedObj[$key] = $cleanedObj[$key];
- }
- if ($debug) {
- var_dump($sortedObj);
- }
- return $sortedObj;
- }
- $items = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/Story05Js dictionary/assets/books/Book Puzzlum's Palace 0.json"), true);
- $cleanedItems = cleanKeys($items, true);
- print_r($cleanedItems);
- file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/Story05Js dictionary/assets/books/Book Puzzlum's Palace Optimized 2.json", json_encode($cleanedItems, JSON_PRETTY_PRINT));
- exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement