3

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 : |

2
  • Use 'prompt' attribute to set default option. <?= Html::activeDropDownList($model, 'username', ArrayHelper::map(Experts::find()->all(), 'id', 'username'), ['prompt' => '--- select'] ) ?> Commented Aug 16, 2017 at 8:33
  • no i mean can not set each value as id of database Commented Aug 16, 2017 at 9:17

1 Answer 1

5

array_merge renumbers numeric array keys. Use +.

<?= Html::activeDropDownList(
    $model, 
    'username',
    ['' => ' '] + ArrayHelper::map(Experts::find()->all(), 'id', 'username')
) ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.