예제 #1
0
 /**
  * Tests Arr::isArray()
  *
  * @test
  * @dataProvider providerIsArray
  * @param mixed   $value     Value to check
  * @param boolean $expected  Is $value an array?
  */
 public function testIsArray($array, $expected)
 {
     $this->assertSame($expected, Arr::isArray($array));
 }
예제 #2
0
 /**
  * Gets a value from an array using a dot separated path.
  *
  *     // Get the value of $array['foo']['bar']
  *     $value = Arr::path($array, 'foo.bar');
  *
  * Using a wildcard "*" will search intermediate arrays and return an array.
  *
  *     // Get the values of "color" in theme
  *     $colors = Arr::path($array, 'theme.*.color');
  *
  *     // Using an array of keys
  *     $colors = Arr::path($array, array('theme', '*', 'color'));
  *
  * @param  array  $array     array to search
  * @param  mixed  $path      key path string (delimiter separated) or array of keys
  * @param  mixed  $default   default value if the path is not set
  * @param  string $delimiter key path delimiter
  * @return mixed
  */
 public static function path($array, $path, $default = null, $delimiter = null)
 {
     if (!Arr::isArray($array)) {
         // This is not an array!
         return $default;
     }
     if (is_array($path)) {
         // The path has already been separated into keys
         $keys = $path;
     } else {
         if (array_key_exists($path, $array)) {
             // No need to do extra processing
             return $array[$path];
         }
         if ($delimiter === null) {
             // Use the default delimiter
             $delimiter = Arr::$delimiter;
         }
         // Remove starting delimiters and spaces
         $path = ltrim($path, "{$delimiter} ");
         // Remove ending delimiters, spaces, and wildcards
         $path = rtrim($path, "{$delimiter} *");
         // Split the keys by delimiter
         $keys = explode($delimiter, $path);
     }
     do {
         $key = array_shift($keys);
         if (isset($array[$key])) {
             if ($keys) {
                 if (Arr::isArray($array[$key])) {
                     // Dig down into the next part of the path
                     $array = $array[$key];
                 } else {
                     // Unable to dig deeper
                     break;
                 }
             } else {
                 // Found the path requested
                 return $array[$key];
             }
         } elseif (ctype_digit($key)) {
             $key = (int) $key;
             if (isset($array[$key])) {
                 if ($keys) {
                     if (Arr::isArray($array[$key])) {
                         // Dig down into the next part of the path
                         $array = $array[$key];
                     } else {
                         // Unable to dig deeper
                         break;
                     }
                 } else {
                     // Found the path requested
                     return $array[$key];
                 }
             } else {
                 // Unable to dig deeper
                 break;
             }
         } elseif ($key === '*') {
             // Handle wildcards
             $values = [];
             foreach ($array as $arr) {
                 if ($value = Arr::path($arr, implode('.', $keys))) {
                     $values[] = $value;
                 }
             }
             if ($values) {
                 // Found the values requested
                 return $values;
             } else {
                 // Unable to dig deeper
                 break;
             }
         } else {
             // Unable to dig deeper
             break;
         }
     } while ($keys);
     // Unable to find the value requested
     return $default;
 }