2011-06-16 22:25:57 -05:00
|
|
|
<?php namespace System;
|
|
|
|
|
|
|
|
|
|
class Arr {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an item from an array.
|
|
|
|
|
*
|
2011-08-02 13:41:57 -05:00
|
|
|
* If the specified key is null, the entire array will be returned. The array may
|
|
|
|
|
* also be accessed using JavaScript "dot" style notation.
|
2011-07-07 23:00:40 -05:00
|
|
|
*
|
2011-06-16 22:25:57 -05:00
|
|
|
* @param array $array
|
2011-06-16 22:27:57 -05:00
|
|
|
* @param string $key
|
2011-07-06 13:45:03 -07:00
|
|
|
* @param mixed $default
|
2011-06-16 22:25:57 -05:00
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2011-06-16 22:27:57 -05:00
|
|
|
public static function get($array, $key, $default = null)
|
2011-06-16 22:25:57 -05:00
|
|
|
{
|
|
|
|
|
if (is_null($key))
|
|
|
|
|
{
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-02 13:38:50 -05:00
|
|
|
foreach (explode('.', $key) as $segment)
|
|
|
|
|
{
|
2011-08-02 13:44:02 -05:00
|
|
|
if ( ! array_key_exists($segment, $array))
|
2011-08-02 13:38:50 -05:00
|
|
|
{
|
|
|
|
|
return is_callable($default) ? call_user_func($default) : $default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array = $array[$segment];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-16 22:25:57 -05:00
|
|
|
}
|