Skip to content
Permalink
Browse files

Avoiding loops for Hash::get() up to 3 levels

Lots of configs and read do not use very deep levels to read. Having the switch to avoid loops can increase the performance in about 30%.
  • Loading branch information...
jrbasso committed Sep 7, 2014
1 parent cc14f4b commit e3633b5d26c372bb1400a11f88b98f0fe00c337a
Showing with 15 additions and 6 deletions.
  1. +15 −6 src/Utility/Hash.php
@@ -57,12 +57,21 @@ public static function get(array $data, $path, $default = null) {
$parts = $path;
}
foreach ($parts as $key) {
if (is_array($data) && isset($data[$key])) {
$data = $data[$key];
} else {
return $default;
}
switch (count($parts)) {
case 1:
return isset($data[$parts[0]]) ? $data[$parts[0]] : $default;
case 2:
return isset($data[$parts[0]][$parts[1]]) ? $data[$parts[0]][$parts[1]] : $default;
case 3:
return isset($data[$parts[0]][$parts[1]][$parts[2]]) ? $data[$parts[0]][$parts[1]][$parts[2]] : $default;
default:
foreach ($parts as $key) {
if (is_array($data) && isset($data[$key])) {
$data = $data[$key];
} else {
return $default;
}
}
}
return $data;

0 comments on commit e3633b5

Please sign in to comment.
You can’t perform that action at this time.