Skip to content

Instantly share code, notes, and snippets.

@nazar-pc
Created November 10, 2015 10:49
Show Gist options
  • Save nazar-pc/f131a19c55390b76d143 to your computer and use it in GitHub Desktop.
Save nazar-pc/f131a19c55390b76d143 to your computer and use it in GitHub Desktop.
<?php
# Creates tree from flat key-value translations
# Can reduce JSON size by 10%, but adds a lot of complexity
# Maybe, it doesn't worth it:(
function make_tree ($input) {
$keys = array_keys($input);
$result = [];
// Step by step create tree by moving one character deeper
while (count($keys)) {
$key = $keys[0];
// Since we use string operations inside - only string keys allowed
if (is_string($key) && strlen($key)) {
$prefix = $key[0];
$similar_keys = array_filter(
$keys,
function ($inner_key) use ($prefix) {
return strlen($inner_key) && $inner_key[0] === $prefix;
}
);
// Having less than 1 key doesn't make any sense
if (count($similar_keys) > 1) {
// Create tree
foreach ($similar_keys as $index => $inner_key) {
$result[$prefix][substr($inner_key, 1) ?: ''] = $input[$inner_key];
unset($keys[$index], $input[$inner_key]);
}
sort($keys);
// Compress obtained tree
$result[$prefix] = make_tree($result[$prefix]);
// Since obtained tree might have ony one key, concat it with current prefix to reduce unnecessary nesting
$count = count($result[$prefix]);
if (
$count == 1 ||
(
$count == 2 &&
is_array(array_values($result[$prefix])[0]) != is_array(array_values($result[$prefix])[1])
)
) {
foreach ($result[$prefix] as $inner_key => $translations) {
$result[$prefix.$inner_key] = $translations;
}
unset($result[$prefix]);
} elseif (is_array($result[$prefix])) {
foreach ($result[$prefix] as $inner_key => $nested_structure) {
if (strlen($inner_key) == 1 && is_array($nested_structure)) {
foreach ($nested_structure as $nested_key => $translations) {
$result[$prefix][$inner_key.$nested_key] = $translations;
}
unset($result[$prefix][$inner_key]);
}
}
}
continue;
}
}
$result[$key] = $input[$key];
array_shift($keys);
unset($input[$key]);
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment