implements array flattening
array_flip() function returns a "Warning: array_flip(): Can only flip STRING and INTEGER values!" when a nested associative array is passed as argument. In case of multiple choices selected entries are not retrieved from form.
I flattened the array before flipping to get them e.g: array("key1" => array("A" => "a", "B" => "b")) becomes array("A" => "a", "B" => "b") and then array("a" => "A", "b" => "B") by flipping
can someone advice ? thanks.
Could you please add a testcase?
I don't understand the need for this feature. Yes, you need to pass a "flat array" to form choices ... but why would you pass a nested array to it? It's like in a Symfony app, right? You must pass a "flat array" yourself ... Symfony doesn't convert it for you.
Also, flattening an array is not only "non trivial" ... but it's also highly dependent on the application. E.g. in case of duplicated keys when flattening, should we keep the first occurrence or the last one?
I'd say we don't need to implement this feature and each app is responsible of generating the right array for the form choices. If I'm wrong, please explain it to me. Thanks!
Common, this is not only a feature. This is first an error when passing a complex array. ChoiceType obviously accepts nested array to display in an proper way "select" tags with sections, while EA not.
Providing nested array gives the opportunity to show a form in a simple way as following:

(e.g. On the picture you get section "Génériques" then its entries, and a section "Officiels" and its entries. Only entries can be selected)
Generally speaking, flattening an array is maybe not trivial, but flattening here is just a way to retrieve the selected entries when showing back a form with choices.
Also, flattening an array is not only "non trivial" ... but it's also highly dependent on the application. E.g. in case of duplicated keys when flattening, should we keep the first occurrence or the last one?
The purpose of flattening the array is here only to retrieve the value associated to a displayed label displayed in the form.
Your question does not make sense, @javiereguiluz.. These choices are unique, it is a direct match between what is shown and the value of "select" entry.
Moreover, this issue also applies without nested array.. You get "User1" and "User2" labels, their values are (let's say) "user1", "user2". It does not make sense to build a form with User1 and User2 label pointing to the same value. This would just be a confusion of the developer.
https://symfony.com/doc/current/reference/forms/types/choice.html#grouping-options
I flattened the array before flipping to get them e.g: array("key1" => array("A" => "a", "B" => "b")) becomes array("A" => "a", "B" => "b") and then array("a" => "A", "b" => "B") by flipping
I think the result should be array("key1" => array("a" => "A", "b" => "B"))
We should keep support for Grouping Options if the choices are grouped.
The only purpose of this function is to be able to retrieve selected element. It doesn't modify the class attributes. So it doesn't really matter. I think you just make the reading of the array more complicate going that way.
I've tested this bug in one of my apps. I use the same choices as your test:
->setChoices([
'foo' => ['A' => 'a', 'B' => 'b'],
'bar' => ['C' => 'c', 'D' => 'd'],
'john' => 'doe'
]);
The result, without applying the changes of this PR, is the following:

So, it works as expected and I don't see any error in the "array flipping" process. What am I missing here?
Did you enable multiple choices ?
I overlooked your message @javiereguiluz. Please just dump() something in src/Field/Configurator/ChoiceConfigurator.php around the array_flip function and share the full lines around "->setChoices()".
NB: Just run the test case with and without applying the changes
easycorp/easyadmin-bundle 3.5.1 still have this problem when using group options:
Warning: array_flip(): Can only flip STRING and INTEGER values!
Replaced by the fix implemented in #5374 and which will be available in the next stable release. Thanks.