Conversation
|
Has been merged... |
Unpacking to access multidimensional arraysIntroductionThis RFC complements the Argument Unpacking RFC. It introduces a syntax for unpacking arrays and Traversables to access multidimensional arrays. Usage examples: $array = [
"foo" => "bar",
42 => 24,
"multi" => [
"dimensional" => [
"array" => "foo"
]
]
];
$select = ["multi", "dimensional", "array"];
//Examples
echo $array[ ...$select ] ?? "Fallback";
is_set( $array[...$select] );
$array[...$select] == "foo";
unset( $array[...$select] );
function array_select(...$args) {
return $array[...$args] ?? null;
}In PHP is natural to use multidimensional arrays, and almost all config files are multidimensional arrays that are not easy to access. AdvantagesWe don't need to know before hand what to select in the array. Example for function array_select(...$args) {
switch (count($args)) {
case 0: return $array; break;
case 1: return $array[$args[0]] ?? null; break;
case 2: return $array[$args[0][$args[1]] ?? null; break;
case 3: return $array[$args[0][$args[1][$args[2]] ?? null; break;
case 4: return $array[$args[0][$args[1][$args[2][$args[3]] ?? null; break;
default:
trigger_error('Maximum 4 levels, selected: '.count($args));
}To get values from a config file (or a multidimensional array), normally it's necessary a function similar to Backward CompatibilityThis change does not break userland or internal compatibility. ... PD: As I don't have access to add RFC's to the wiki I placed this here. Thank you. |
|
@nikic Please could you review this new feature. Or if you could say me where can I send this new feature for review. |
|
@joanhey Please open another pull request with an implementation or raise this issue on internals. |
Implementation for the argument unpacking RFC.