Skip to content

Conditionally setting array key only when it exists in the other array #7

@boryn

Description

@boryn

Hi!

I came across your library and I am gradually implementing it in the code base :)

I have a question, if somehow library covers this example?

if (!empty($someExternalArray['picture']['data']['url'])) {
    $myArray['picture'] = $someExternalArray['picture']['data']['url'];
}

Is the setItem() designed for it? I'm not sure.

Maybe something like this would cover this need?
setConditionally($myArray, 'picture', $someExternalArray, 'picture.data.url'])

Of course, I know I can write myself such a helper, but it would be nice if library covered it as well.

The main key is not to clutter the $myArray with keys which we know will be empty, that's why I don't like this approach (and look for a simpler solution than the original if):
$myArray['picture'] = $someExternalArray['picture']['data']['url'] ?? null;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions