mako\Arr


Description


Array helper.


Class methods


Toggle source

protected __construct()


Protected constructor since this is a static class.

Return value

NULL

protected function __construct()
{
	// Nothing here
}

Toggle source

public static get($array, $path, $default = NULL)


Returns value from array using "dot notation".


Parameters

Type Description
array Array we're going to search
string Array path
mixed Default return value
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;
}

Toggle source

public static set(&$array, $path, $value)


Sets an array value using "dot notation".


Parameters

Type Description
array Array you want to modify
string Array path
mixed Value to set
Return value

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;
}

Toggle source

public static delete(&$array, $path)


Deletes an array value using "dot notation".


Parameters

Type Description
array Array you want to modify
string Array path
Return value

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;
}

Toggle source

public static random($array)


Returns a random value from an array.


Parameters

Type Description
array Array you want to pick a random value from
Return value

mixed

public static function random(array $array)
{
	return $array[array_rand($array)];
}