<?php
function getValueByPath(array &$array, $path)
{
if (is_string($path)) {
if (strpos($path, '\\.') === false) {
$path = explode('.', $path);
} else {
$pattern = '#(?<!\\\\)((?:\\\\\\\\)*)\\.#';
$split = preg_split($pattern, $path, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
$split = array_reduce(
$split,
static function ($result, $value) {
$lastIndex = count($result) - 1;
if ($lastIndex >= 0 && preg_match('#^\\\\*$#', $value)) {
$result[$lastIndex] = $result[$lastIndex] . $value;
} else {
$result[] = $value;
}
return $result;
},
[]
);
$path = array_map(
static function ($value) {
return str_replace(['\\\\','\\.'], ['\\','.'], $value);
},
$split
);
}
} elseif (!is_array($path)) {
throw new \InvalidArgumentException('getValueByPath() expects $path to be string or array, "' . gettype($path) . '" given.', 1304950007);
}
$key = array_shift($path);
if (isset($array[$key])) {
if (count($path) > 0) {
return (is_array($array[$key])) ? getValueByPath($array[$key], $path) : null;
} else {
return $array[$key];
}
} else {
return null;
}
}
function getValueByPathOriginal(array &$array, $path)
{
if (is_string($path)) {
$path = explode('.', $path);
} elseif (!is_array($path)) {
throw new \InvalidArgumentException('getValueByPath() expects $path to be string or array, "' . gettype($path) . '" given.', 1304950007);
}
$key = array_shift($path);
if (isset($array[$key])) {
if (count($path) > 0) {
return (is_array($array[$key])) ? getValueByPath($array[$key], $path) : null;
} else {
return $array[$key];
}
} else {
return null;
}
}
$array = [
'foo' => [
'bar' => [
'baz' => 'quux'
],
'bar.baz' => 'quux'
]
];
$path = 'foo.bar.baz';
$time_start = microtime(true);
for ($i = 0; $i < 50000; $i++) {
getValueByPathOriginal($array, $path);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Original took $time seconds\n";
$time_start = microtime(true);
for ($i = 0; $i < 50000; $i++) {
getValueByPath($array, $path);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Changed $time seconds\n";
$path = 'foo.bar\.baz';
$time_start = microtime(true);
for ($i = 0; $i < 50000; $i++) {
getValueByPath($array, $path);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Changed regex $time seconds\n";
- Output for 8.1.6
- Original took 0.031779050827026 seconds
Changed 0.0329430103302 seconds
Changed regex 0.084174871444702 seconds
- Output for 8.1.5
- Original took 0.032978057861328 seconds
Changed 0.033874988555908 seconds
Changed regex 0.080811977386475 seconds
- Output for 8.1.4
- Original took 0.035765171051025 seconds
Changed 0.035123109817505 seconds
Changed regex 0.080214977264404 seconds
- Output for 8.1.3
- Original took 0.034018993377686 seconds
Changed 0.033885955810547 seconds
Changed regex 0.082050085067749 seconds
- Output for 8.1.2
- Original took 0.030797958374023 seconds
Changed 0.032464027404785 seconds
Changed regex 0.078741788864136 seconds
- Output for 8.1.1
- Original took 0.03150486946106 seconds
Changed 0.033253908157349 seconds
Changed regex 0.078613996505737 seconds
- Output for 8.1.0
- Original took 0.030423879623413 seconds
Changed 0.031811952590942 seconds
Changed regex 0.078656196594238 seconds
- Output for 8.0.19
- Original took 0.033373117446899 seconds
Changed 0.035452127456665 seconds
Changed regex 0.082594156265259 seconds
- Output for 8.0.18
- Original took 0.032855033874512 seconds
Changed 0.033979177474976 seconds
Changed regex 0.084949016571045 seconds
- Output for 8.0.17
- Original took 0.032563924789429 seconds
Changed 0.033122062683105 seconds
Changed regex 0.080176115036011 seconds
- Output for 8.0.16
- Original took 0.032495021820068 seconds
Changed 0.034256935119629 seconds
Changed regex 0.080247163772583 seconds
- Output for 8.0.15
- Original took 0.032351970672607 seconds
Changed 0.033906936645508 seconds
Changed regex 0.081335067749023 seconds
- Output for 8.0.14
- Original took 0.033119916915894 seconds
Changed 0.033963918685913 seconds
Changed regex 0.079931974411011 seconds
- Output for 8.0.13
- Original took 0.032212972640991 seconds
Changed 0.033811092376709 seconds
Changed regex 0.081732988357544 seconds
- Output for 8.0.12
- Original took 0.03358006477356 seconds
Changed 0.034240961074829 seconds
Changed regex 0.084712982177734 seconds
- Output for 8.0.11
- Original took 0.033141851425171 seconds
Changed 0.034549951553345 seconds
Changed regex 0.083502769470215 seconds
- Output for 8.0.10
- Original took 0.032840013504028 seconds
Changed 0.035053968429565 seconds
Changed regex 0.081979990005493 seconds
- Output for 8.0.9
- Original took 0.035301923751831 seconds
Changed 0.035585165023804 seconds
Changed regex 0.092816114425659 seconds
- Output for 8.0.8
- Original took 0.035759925842285 seconds
Changed 0.037413120269775 seconds
Changed regex 0.092957973480225 seconds
- Output for 8.0.7
- Original took 0.036612987518311 seconds
Changed 0.037336111068726 seconds
Changed regex 0.090250015258789 seconds
- Output for 8.0.6
- Original took 0.037590980529785 seconds
Changed 0.042234897613525 seconds
Changed regex 0.086982011795044 seconds
- Output for 8.0.5
- Original took 0.034763097763062 seconds
Changed 0.03929615020752 seconds
Changed regex 0.098753929138184 seconds
- Output for 8.0.3
- Original took 0.038388013839722 seconds
Changed 0.038419961929321 seconds
Changed regex 0.090431928634644 seconds
- Output for 8.0.2
- Original took 0.033869028091431 seconds
Changed 0.035923957824707 seconds
Changed regex 0.098061800003052 seconds
- Output for 8.0.1
- Original took 0.034785985946655 seconds
Changed 0.039479970932007 seconds
Changed regex 0.092697143554688 seconds
- Output for 7.4.29
- Original took 0.037293910980225 seconds
Changed 0.040235996246338 seconds
Changed regex 0.086713075637817 seconds
- Output for 7.4.28
- Original took 0.035547018051147 seconds
Changed 0.043584823608398 seconds
Changed regex 0.085545063018799 seconds
- Output for 7.4.27
- Original took 0.032569885253906 seconds
Changed 0.0345458984375 seconds
Changed regex 0.08082103729248 seconds
- Output for 7.4.26
- Original took 0.033699989318848 seconds
Changed 0.035513162612915 seconds
Changed regex 0.08461594581604 seconds
- Output for 7.4.25
- Original took 0.032359838485718 seconds
Changed 0.03387188911438 seconds
Changed regex 0.078391075134277 seconds
- Output for 7.4.24
- Original took 0.03374195098877 seconds
Changed 0.035871982574463 seconds
Changed regex 0.079500913619995 seconds
- Output for 7.4.23
- Original took 0.033695220947266 seconds
Changed 0.036165952682495 seconds
Changed regex 0.090667009353638 seconds
- Output for 7.4.22
- Original took 0.033817052841187 seconds
Changed 0.036226987838745 seconds
Changed regex 0.085537195205688 seconds
- Output for 7.4.21
- Original took 0.034192800521851 seconds
Changed 0.034717082977295 seconds
Changed regex 0.082439184188843 seconds
- Output for 7.4.20
- Original took 0.036329984664917 seconds
Changed 0.037317037582397 seconds
Changed regex 0.084820032119751 seconds
- Output for 7.4.19
- Original took 0.033701181411743 seconds
Changed 0.035079956054688 seconds
Changed regex 0.080782890319824 seconds
- Output for 7.4.18
- Original took 0.034045934677124 seconds
Changed 0.035490989685059 seconds
Changed regex 0.081369161605835 seconds
- Output for 7.4.16
- Original took 0.032811164855957 seconds
Changed 0.034533023834229 seconds
Changed regex 0.080883026123047 seconds
- Output for 7.4.15
- Original took 0.033374786376953 seconds
Changed 0.03460693359375 seconds
Changed regex 0.082374095916748 seconds
- Output for 7.4.14
- Original took 0.032991170883179 seconds
Changed 0.035272121429443 seconds
Changed regex 0.083690881729126 seconds
- Output for 7.4.13
- Original took 0.032709836959839 seconds
Changed 0.03491997718811 seconds
Changed regex 0.085123062133789 seconds
- Output for 7.4.12
- Original took 0.033352851867676 seconds
Changed 0.035406112670898 seconds
Changed regex 0.079153060913086 seconds
- Output for 7.4.11
- Original took 0.033093929290771 seconds
Changed 0.034492015838623 seconds
Changed regex 0.080765008926392 seconds
- Output for 7.4.10
- Original took 0.033267021179199 seconds
Changed 0.035268068313599 seconds
Changed regex 0.077854156494141 seconds
- Output for 7.4.9
- Original took 0.031280994415283 seconds
Changed 0.033027172088623 seconds
Changed regex 0.078237056732178 seconds
- Output for 7.4.8
- Original took 0.032557010650635 seconds
Changed 0.034488916397095 seconds
Changed regex 0.078229904174805 seconds
- Output for 7.4.7
- Original took 0.0329270362854 seconds
Changed 0.034588098526001 seconds
Changed regex 0.080631971359253 seconds
- Output for 7.4.6
- Original took 0.033050060272217 seconds
Changed 0.035138130187988 seconds
Changed regex 0.083417892456055 seconds
- Output for 7.4.5
- Original took 0.034710168838501 seconds
Changed 0.037014961242676 seconds
Changed regex 0.081784963607788 seconds
- Output for 7.4.4
- Original took 0.034659862518311 seconds
Changed 0.036258935928345 seconds
Changed regex 0.08699893951416 seconds
- Output for 7.4.3
- Original took 0.033619165420532 seconds
Changed 0.035187005996704 seconds
Changed regex 0.081651926040649 seconds
- Output for 7.4.2
- Original took 0.032735109329224 seconds
Changed 0.034717082977295 seconds
Changed regex 0.079301118850708 seconds
- Output for 7.4.1
- Original took 0.033937215805054 seconds
Changed 0.036170959472656 seconds
Changed regex 0.0798659324646 seconds
- Output for 7.4.0
- Original took 0.034034013748169 seconds
Changed 0.03584098815918 seconds
Changed regex 0.081017971038818 seconds
preferences:
94.27 ms | 401 KiB | 59 Q