Array helper.
Protected constructor since this is a static class.
NULL
protected function __construct()
{
// Nothing here
}
Returns value from array using "dot notation".
| Type | Description |
|---|---|
| array | Array we're going to search |
| string | Array path |
| mixed | Default return value |
mixed
public static function get(array $array, $path, $default = null)
{
$segments = explode('.', $path);
foreach($segments as $segment)
{
if(!is_array($array) || !isset($array[$segment]))
{
return $default;
}
$array = $array[$segment];
}
return $array;
}
Sets an array value using "dot notation".
| Type | Description |
|---|---|
| array | Array you want to modify |
| string | Array path |
| mixed | Value to set |
NULL
public static function set(array & $array, $path, $value)
{
$segments = explode('.', $path);
while(count($segments) > 1)
{
$segment = array_shift($segments);
if (!isset($array[$segment]) || !is_array($array[$segment]))
{
$array[$segment] = array();
}
$array =& $array[$segment];
}
$array[array_shift($segments)] = $value;
}
Deletes an array value using "dot notation".
| Type | Description |
|---|---|
| array | Array you want to modify |
| string | Array path |
NULL
public static function delete(array & $array, $path)
{
$segments = explode('.', $path);
while(count($segments) > 1)
{
$segment = array_shift($segments);
if (!isset($array[$segment]) || ! is_array($array[$segment]))
{
return false;
}
$array =& $array[$segment];
}
unset($array[array_shift($segments)]);
return true;
}
Returns a random value from an array.
| Type | Description |
|---|---|
| array | Array you want to pick a random value from |
mixed
public static function random(array $array)
{
return $array[array_rand($array)];
}