PHP: How to set an array element without any extra code?

Good day
There is a piece of code for SELECT fields (YII2 framework)

->dropDownList($structure_enum, [
 'class' => 'form__select',
 'style' => 'width: 100%;',
])


Ie roughly speaking, there is an array of data
As this array to pass or not another key is the condition, not the city the extra code above
Ie write something like

->dropDownList($structure_enum, [
 'class' => 'form__select',
 'style' => 'width: 100%;',
 $structure_strict ? 'disabled' => 'disabled' : ",
])

The code itself is not working, but for example clear

P. S Please do not offer the type of option
$a = [
 'class' => 'form__select',
 'style' => 'width: 100%;',
];

if ($structure_strict) $a ['disabled'] = 'disabled';
.....
->dropDownList($structure_enum, $a)

Am interested in the question of the brevity of the record

P. S
Thank you all for the answers
In General or using array_merge or through the addition of arrays
July 9th 19 at 11:16
3 answers
July 9th 19 at 11:18
Solution
As an option to glue the arrays:
->dropDownList($structure_enum, array_merge(
 [ 'class' => 'form__select', 'style' => 'width: 100%;' ],
 ($structure_strict) ? [ 'disabled' => 'disabled' ] : []
))
Thanks for the reply, also thought in this direction
Still just as cumbersome
But if there are more elegant solutions, I will do so :) - malvina.Yundt commented on July 9th 19 at 11:21
Now digging through the code YII2
In the General case, applicable to Your version
But if you use YII2, then purely for me, you can specify false as the value of the disabled element will not be created :)
In General, live and learn ) - malvina.Yundt commented on July 9th 19 at 11:24
July 9th 19 at 11:20
Solution
In CleverStyle Framework can and 'disabled' => $structure_strict, and even $structure_strict ? 'disabled' : falseif Yii2 will not allow it - then you can email the developers/implement yourself and send a PR, should be fairly trivial in terms of implementation.
Please re-read the question
I have no problem to set the value of the item
I have a problem to create the element or not - malvina.Yundt commented on July 9th 19 at 11:23
: Most of the questions are not abstract, but very specific scope. Based on the experience of using the toaster many people ask not what they need.
In this case, you have a direct connection with Yii2. Apparently, Yii2 supports some convenient features that could support. That you and I have offered examples of how this is implemented in other projects, and how you can solve this inconvenience for yourself and other developers, improving Yii2.

I apologize if my answer to you is absolutely irrelevant in its original form.

If dry in fact - to ask and not to ask the item at the same time we don't have the condition.
If you Dodge then you can try the following:
->dropDownList($structure_enum, array_filter([
 'class' => 'form__select',
 'style' => 'width: 100%;',
 'disabled' => $structure_strict ? 'disabled' : ",
]))

But you have to understand that you have removed all the items that are false, but in your case this may be sufficient. - malvina.Yundt commented on July 9th 19 at 11:26
Thank you for your attention
I mentioned in a response (other than your) comment
Ie specifically for YII2 Yes, you can assign 'disabled => false (it is false and not an empty string)
And then YII2 in the results HTML will ignore this element, ie, the idea is what you need and
But originally the question is about arrays - ie how to create or not create an array element
Oh well, problem solved
Thanks for your help, Your version made the source code for YII2 to search through and see what the output is ) - malvina.Yundt commented on July 9th 19 at 11:29
And Yes, I would like to know the solution in General, not in relation to YII2 ) - Roger.Erns commented on July 9th 19 at 11:32
: Then try this:
->dropDownList($structure_enum, [
 'class' => 'form__select',
 'style' => 'width: 100%;'
] + ($structure_strict ? ['disabled' => 'disabled'] : []))


Unless you have duplicate numeric keys in the array, then array_merge() and its overhead you don't need. - malvina.Yundt commented on July 9th 19 at 11:35
Exactly the same also can be...
I even forgot about adding the arrays...
Sorry two options to note the decision (
In General, thanks for the help, not to forget it ) - malvina.Yundt commented on July 9th 19 at 11:38
: I think two answers may be noted at the same time. Unless something has changed recently in the Toaster. - Roger.Erns commented on July 9th 19 at 11:41
Sorry, didn't know why I thought that only one
Noted ) - malvina.Yundt commented on July 9th 19 at 11:44
July 9th 19 at 11:22
<?php

--->dropDownList($structure_enum, array_filter([
 'class' => 'form__select',
 'style' => 'width: 100%;',
 'disabled' => $structure_strict ? 'disabled' : false,
]));

Find more questions by tags YiiArraysPHP