Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the functionality of setValue #136

Closed
Kutuzovska opened this issue Nov 5, 2023 · 3 comments
Closed

Extend the functionality of setValue #136

Kutuzovska opened this issue Nov 5, 2023 · 3 comments

Comments

@Kutuzovska
Copy link
Contributor

How do you like the idea of extending the functionality of setValue by adding an append parameter or similar?
Example:

$arr = [
    'вот' => [
        'мой' => [
            'ключ' => null,
        ],
    ],
];

$result = \Yiisoft\Arrays\ArrayHelper::getValue($arr, ['вот', 'мой', 'ключ'], []);
echo $result; // [];

\Yiisoft\Arrays\ArrayHelper::setValue($arr, ['вот', 'мой', 'ключ'], 'lol');
\Yiisoft\Arrays\ArrayHelper::setValue($arr, ['вот', 'мой', 'ключ'], 'kek');

$result = \Yiisoft\Arrays\ArrayHelper::getValue($arr, ['вот', 'мой', 'ключ'], []);
echo $result; // 'kek';

And adding append

\Yiisoft\Arrays\ArrayHelper::setValue($arr, ['вот', 'мой', 'ключ'], 'lol', append: true);
\Yiisoft\Arrays\ArrayHelper::setValue($arr, ['вот', 'мой', 'ключ'], 'kek', append: true);

$result = \Yiisoft\Arrays\ArrayHelper::getValue($arr, ['вот', 'мой', 'ключ'], []);
echo $result; // ['lol', 'kek'];

If the key does not exist an empty array is initialized.

@Tigrov
Copy link
Member

Tigrov commented Nov 5, 2023

Possible to realize this way:

\Yiisoft\Arrays\ArrayHelper::setValue($arr, ['вот', 'мой', 'ключ', null], 'lol');
\Yiisoft\Arrays\ArrayHelper::setValue($arr, ['вот', 'мой', 'ключ', null], 'kek');

$result = \Yiisoft\Arrays\ArrayHelper::getValue($arr, ['вот', 'мой', 'ключ'], []);
echo $result; // ['lol', 'kek'];

or

\Yiisoft\Arrays\ArrayHelper::setValue($arr, ['вот', 'мой', 'ключ', []], 'lol');
\Yiisoft\Arrays\ArrayHelper::setValue($arr, ['вот', 'мой', 'ключ', []], 'kek');

$result = \Yiisoft\Arrays\ArrayHelper::getValue($arr, ['вот', 'мой', 'ключ'], []);
echo $result; // ['lol', 'kek'];

@vjik
Copy link
Member

vjik commented Nov 5, 2023

I think better create new method for this feature. Call it addValue or push...

@vjik
Copy link
Member

vjik commented Nov 7, 2023

Done by #137

@vjik vjik closed this as completed Nov 7, 2023
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

No branches or pull requests

3 participants