areff areff - 1 year ago 269
PHP Question

How to set value in array helper Yii2?

I want to set value of array helper in Yii 2 as id (primary key of database).
But when I use it shows me 0,1,2,3,.. not its real id number.

Here is my array helper code:

<?= Html::activeDropDownList($model, 'username',
array_merge(array(''=>' '), ArrayHelper::map(Experts::find()->all(), 'id', 'username'))) ?>


How can I access real value of primary id??

When I use other parameter it works correctly but when I use id it shows number from 0 to up : |

Answer Source

array_merge renumbers numeric array keys. Use +.

<?= Html::activeDropDownList(
    $model, 
    'username',
    ['' => ' '] + ArrayHelper::map(Experts::find()->all(), 'id', 'username')
) ?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download