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

Merge does not work when providing numerical array keys #1996

Closed
ghost opened this issue Feb 24, 2016 · 4 comments
Closed

Merge does not work when providing numerical array keys #1996

ghost opened this issue Feb 24, 2016 · 4 comments

Comments

@ghost
Copy link

@ghost ghost commented Feb 24, 2016

Hi all,

Merge filter does not work when trying to provide numerical array keys.

If the key is 0 or "0", it will NOT be replaced, however if a string array key is used such as "someKey", the actual value matching "someKey" is replaced.

What does not work:
{% set someVar = someVar|merge({ (key) : someVar.someValue }) %}

This may be a bug with PHP array_merge function, if so, should it be reported?

@stof

This comment has been minimized.

Copy link
Member

@stof stof commented Feb 25, 2016

It is not a bug of array_merge but a feature. Numeric keys are merged by considering the array as a list.
This is why there is another function named array_replace in PHP.

@ghost

This comment has been minimized.

Copy link
Author

@ghost ghost commented Feb 25, 2016

This behavior doesn't seem logical to me. Not to me and I'm not the only one having this opinion.

Is array_replace usable in Twig for numerical indexes?

Le 25 févr. 2016 à 16:35, Christophe Coevoet notifications@github.com a écrit :

It is not a bug of array_merge but a feature. Numeric keys are merged by considering the array as a list.
This is why there is another function named array_replace in PHP.


Reply to this email directly or view it on GitHub.

@fabpot

This comment has been minimized.

Copy link
Contributor

@fabpot fabpot commented Feb 26, 2016

Closing as Twig is just replicating the PHP feature here.

@fabpot fabpot closed this Feb 26, 2016
@TyrionGraphiste

This comment has been minimized.

Copy link

@TyrionGraphiste TyrionGraphiste commented Aug 30, 2018

Actually this is a bug because you provide no other option to solve this issue. Expect that every developer having this issue change to string key is not a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.