- Get a value by using a multi-part key
- Set a value by using a multi-part key
- Remove a value by using a multi-part key
- Transform a multi-dimension array into a one-dimension array with multi-part key
The class is covered at 100% with PHPUnit.
The class implements the following interfaces:
\ArrayAccessfor access (read/write/isset/unset) like an array\IteratorAggregateforforeachuse\Countableto get the number of values (usable with PHPcount()function)\Serializable
Considering the following array:
{
"application": {
"service": {
"mail": {
"unix": "sendmail"
}
}
}
}To get the value sendmail a traditional way is:
- ask for the array
application, - then ask for the sub array
service, - then ask for the sub array
mail, - and finally ask for the value of
unix.
In PHP code i will look like:
$value = $array['application']['service']['mail']['unix']The idea behind the Multi-part key, is to transform multi-level array call into just this:
$value = $array['application.service.mail.unix'];Yeah :) But I also have a unique identifier for the value. It's also way easier to dynamically build a multi-part key than change the multi-level call.
It's way more easier to manipulate a one-dimension array, than play with recursion.
The class instance constructor.
The default value for $data is an empty array.
The default value for $keySeparator is "."
The $data parameter is the initial array.
Get the multi-part key separator.
Set the multi-part key separator
Static method to flatten a multi-dimension array.
The Default value for $prefix is an empty string
The Default value for $keySeparator is "."
The $prefix parameter allow you to add one (or more) key part(s) at the beginning of all multi-part key.
The flatten process will create a new array.
array(
"application" => array(
"service" => array(
"mail" => array(
"unix": "sendmail",
"stmp": "127.0.0.1"
)
)
)
);
// Flatten to (with default options)
array(
"application.service.mail.unix" => "sendmail",
"application.service.mail.smtp" => "127.0.0.1"
);Flatten the current object according to the keySeparator.
The method return a new array.
See ::flattenArray for more details.
Static method to un-flatten a multi-dimension array.
The Default value for $keySeparator is "."
The un-flatten process will create a new multi-dimension array:
array(
"database.mysql.host" = "127.0.0.1",
"database.mysql.port" = 3306,
"database.sqlite.file" = "db.sqlite",
"cache.redis.host" = "localhost"
);
// Un-flatten to (with default keySeparator)
array(
"database" => array(
"mysql" => array(
"host" => "127.0.0.1",
"port" => 3306
),
"sqlite" => array(
"file" => "db.sqlite"
)
),
"cache" => array(
"redis" => array(
"host" => "localhost"
)
)
);Merge an array with the object values. The new value can override existing value if $override is set to true
The Default value for $prefix is an empty string
The Default value for $override is false
Get a value withe a multi-part key.
If the value does not exist, null will be return.
Check if a multi-part key is defined.
Add a value.
Remove a value.
Return true if the value is removed, false is the value does not exist.
Return the array representation of the class.
The Default value for $flatten is false
If $flatten is set to true then the flatten version is return (see flatten and ::flattenArray for mor details)