I
I
IdFox2016-05-27 03:07:47
Yii
IdFox, 2016-05-27 03:07:47

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

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

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

That is, roughly speaking, there is an array of data
How to transfer or not one more key to this array by condition, without fencing extra code above
I.e. write something like
->dropDownList($structure_enum, [
    'class' => 'form__select',
    'style' => 'width: 100%;',
    $structure_strict ? 'disabled' => 'disabled' : '',
])

The code itself is not working, but for the example it is understandable
PS Please do not offer a type option in the answers
$a = [
    'class' => 'form__select',
    'style' => 'width: 100%;',
];

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

Interested in the issue of brevity
PS
Thank you all for your answers
In general, either through array_merge or through array addition

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2016-05-27
@IdFox

As an option to glue arrays:

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

S
Sergey Semenko, 2016-05-27
@abler98

<?php

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question