Skip to content

Conversation

@nikic
Copy link
Member

@nikic nikic commented Sep 30, 2013

Implementation for the argument unpacking RFC.

@nikic
Copy link
Member Author

nikic commented Jan 18, 2014

Has been merged...

@nikic nikic closed this Jan 18, 2014
@joanhey
Copy link
Contributor

joanhey commented Aug 20, 2019

Unpacking to access multidimensional arrays

Introduction

This 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.

Advantages

We don't need to know before hand what to select in the array.

Example for array_select() without unpacking:

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 array_select().

Backward Compatibility

This 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.
And hopping that nikic or anybody can include it.

Thank you.

@joanhey
Copy link
Contributor

joanhey commented Aug 23, 2019

@nikic Please could you review this new feature.

Or if you could say me where can I send this new feature for review.
Thanks

@Girgias
Copy link
Member

Girgias commented Aug 23, 2019

@joanhey Please open another pull request with an implementation or raise this issue on internals.
An old PR is not the place to raise questions like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants