Skip to content

Get array value by matcher #135

@yus-ham

Description

@yus-ham

What the problem?

arrays/src/ArrayHelper.php

Lines 210 to 212 in c3d14a3

if ($key instanceof Closure) {
return $key($array, $default);
}

Current implementation of getValue($array, $key) with $key as anonymous function is not match with the phrases Retrieves the value of an array element. basically it just like a value transformation, it can returns anything even value from outside array itself as of in example

arrays/src/ArrayHelper.php

Lines 184 to 188 in c3d14a3

* // Working with anonymous function:
* $fullName = \Yiisoft\Arrays\ArrayHelper::getValue($user, function ($user, $defaultValue) {
* return $user->firstName . ' ' . $user->lastName;
* });
*

What is the expected result?

the implementation should search value of an array by matcher function

    if ($key instanceof Closure) {
        foreach ($array as $key => $value) {
            if ($key($value, $key)) {
                return $value;
            }
        }

        return $default;
    }

and then, change the signature of matcher function to be function($value, $key): bool .

Additional info

Q A
Version 1.0.?
PHP version
Operating system

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions