Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improvements to Hash::expand and Hash::merge.
Because of the recursion in these functions, processing very large
arrays would take a very long time. I rewrote the functions to
eliminate any unnecessary recursion and function calls. Large arrays
are now processed much faster.
  • Loading branch information
hperrin committed Oct 1, 2014
1 parent 348b10e commit 00bc228
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions src/Utility/Hash.php
Expand Up @@ -653,6 +653,9 @@ public static function flatten(array $data, $separator = '.') {
*/
public static function expand(array $data, $separator = '.') {
$result = array();

$stack = array();

foreach ($data as $flat => $value) {
$keys = explode($separator, $flat);
$keys = array_reverse($keys);
Expand All @@ -665,7 +668,24 @@ public static function expand(array $data, $separator = '.') {
$k => $child
);
}
$result = static::merge($result, $child);

$stack[] = array($child, &$result);

while (!empty($stack)) {
foreach ($stack as $curKey => &$curMerge) {
foreach ($curMerge[0] as $key => &$val) {
if (!empty($curMerge[1][$key]) && (array)$curMerge[1][$key]===$curMerge[1][$key] && (array)$val===$val) {
$stack[] = array(&$val, &$curMerge[1][$key]);
} elseif ((int)$key===$key && isset($curMerge[1][$key])) {
$curMerge[1][] = $val;
} else {
$curMerge[1][$key] = $val;
}
}
unset($stack[$curKey]);
}
unset($curMerge);
}
}
return $result;
}
Expand All @@ -685,19 +705,28 @@ public static function expand(array $data, $separator = '.') {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::merge
*/
public static function merge(array $data, $merge) {
$args = func_get_args();
$return = current($args);

while (($arg = next($args)) !== false) {
foreach ((array)$arg as $key => $val) {
if (!empty($return[$key]) && is_array($return[$key]) && is_array($val)) {
$return[$key] = static::merge($return[$key], $val);
} elseif (is_int($key) && isset($return[$key])) {
$return[] = $val;
} else {
$return[$key] = $val;
$args = array_slice(func_get_args(), 1);
$return = $data;

foreach ($args as &$curArg) {
$stack[] = array((array)$curArg, &$return);
}
unset($curArg);

while (!empty($stack)) {
foreach ($stack as $curKey => &$curMerge) {
foreach ($curMerge[0] as $key => &$val) {
if (!empty($curMerge[1][$key]) && (array)$curMerge[1][$key]===$curMerge[1][$key] && (array)$val===$val) {
$stack[] = array(&$val, &$curMerge[1][$key]);
} elseif ((int)$key===$key && isset($curMerge[1][$key])) {
$curMerge[1][] = $val;
} else {
$curMerge[1][$key] = $val;
}
}
unset($stack[$curKey]);
}
unset($curMerge);
}
return $return;
}
Expand Down

0 comments on commit 00bc228

Please sign in to comment.