0

I have following code in my model:

 public function getData($property)
    {
        $data = array(
            'a_4_1' => array(
               'RUB',
                'USD',
                'JPY',
            ),

        );
        return $data[$property];
    }

And in my view:

<?= $form->labelEx($model, 'a_4_1', array('class' => 'col-xs-12 col-sm-2 control-label')) ?>
    <div class="col-xs-12 col-sm-3">
        <?= $form->dropDownList($model, 'a_4_1',$model->getData('d_4_1'), array('class' => 'form-control')) ?>
        <?= $form->error($model, 'a_4_1') ?>
    </div>

When I save it to the database, it saves data in the integer format (e.g 1,2,..) I need to save array elements' names to the database(e.g RUB, USD)(not integer numbers). How can I do it?

2
  • Controller code please Commented Dec 28, 2016 at 11:06
  • It is not related to controller Commented Dec 28, 2016 at 11:24

1 Answer 1

1

use array_combine() to change key value of your array like below:

array_combine($model->getData('d_4_1'),$model->getData('d_4_1'))

<?= $form->labelEx($model, 'a_4_1', array('class' => 'col-xs-12 col-sm-2 control-label')) ?>
    <div class="col-xs-12 col-sm-3">
        <?= $form->dropDownList($model, 'a_4_1',array_combine($model->getData('d_4_1'),$model->getData('d_4_1')), array('class' => 'form-control')) ?>
        <?= $form->error($model, 'a_4_1') ?>
</div>
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.