Ejemplo n.º 1
0
 /**
  * Get the resulting value of an attempt to traverse a key path.
  *
  * Each key in the path is separated with a dot.
  *
  * For example, the following snippet should return `true`:
  * ```php
  * Arr::dotGet([
  *     'hello' => [
  *         'world' => true,
  *     ],
  * ], 'hello.world');
  * ```
  *
  * Additionally, a default value may be provided, which will be returned if
  * the path does not yield to a value.
  *
  * @param array $array
  * @param string $key
  * @param null|mixed $default
  *
  * @return mixed
  */
 public static function dotGet(array $array, $key, $default = null)
 {
     if (is_null($key)) {
         return $array;
     }
     if (isset($array[$key])) {
         return $array[$key];
     }
     foreach (explode('.', $key) as $segment) {
         if (!is_array($array) || !array_key_exists($segment, $array)) {
             return Std::thunk($default);
         }
         $array = $array[$segment];
     }
     return $array;
 }