Arrays
defaultAttributes
Use a default array and pass in overriding keys if there is a key from the attributes array in the defaults array.
Arrays::defaultAttributes(array $defaults = [], array $attributes = [])
Example
Replace key from defaults with one found in the attributes:
use \Pbc\Bandolier\Type\Arrays
$defaults = ["foo" => "bar", "bazz" => "bin"];
$attributes = ["bazz" => "green"];
Arrays::defaultAttributes($defaults, $attributes);
// Returns ["foo" => "bar","bazz" => "green"]
Key from attributes was not found in defaults so the defaults will be returned:
use \Pbc\Bandolier\Type\Arrays
$defaults = ["foo" => "bar", "bazz" => "bin"];
$attributes = ["some" => "unknown key"];
Arrays::defaultAttributes($defaults, $attributes);
// Returns ["foo" => "bar","bazz" => "bin"]
getAttribute
Get an value from array by key. If none was found use a default.
Arrays::getAttribute(array $data, $attribute = null, $default = null)
Example
Get value from array by key:
use \Pbc\Bandolier\Type\Arrays
$list = ["foo" => "bar", "bazz" => "bin"];
$attribute = "foo"
Arrays::getAttribute($list, $attribute);
// Returns "bar"
A default value value is returned if key is not found:
use \Pbc\Bandolier\Type\Arrays
$list = ["foo" => "bar", "bazz" => "bin"];
$attribute = "red"
Arrays::getAttribute($list, $attribute, "default");
// Returns "default"
getKey
Get an key from array by value. if none was found use a default.
getKey(array $data, $value, $default=null)
Example
Get a key from array by value:
use \Pbc\Bandolier\Type\Arrays
$list = ["foo" => "bar", "bazz" => "bin"];
$value = "bar"
Arrays::getKey($list, $value);
// Returns "foo"
A default key value is returned if value is not found:
use \Pbc\Bandolier\Type\Arrays
$list = ["foo" => "bar", "bazz" => "bin"];
$value = "red"
Arrays::getKey($list, $value, "default");
// Returns "default"