Skip to content

Commit

Permalink
[7.x] 3.5x faster micro performance enhancement for data_get() (#32192)
Browse files Browse the repository at this point in the history
* Replace while:array_shift with foreach:unset

Operation is 10x faster

* fix code style

* fix code style

* add null check

* change `$segment===null` to `is_null($segment)`
  • Loading branch information
kevindees committed Apr 3, 2020
1 parent de4a666 commit 55189c1
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ function data_get($target, $key, $default = null)

$key = is_array($key) ? $key : explode('.', $key);

while (! is_null($segment = array_shift($key))) {
foreach ($key as $i => $segment) {
unset($key[$i]);

if (is_null($segment)) {
return $target;
}

if ($segment === '*') {
if ($target instanceof Collection) {
$target = $target->all();
Expand Down

0 comments on commit 55189c1

Please sign in to comment.