Yii - How to get a values array from an Active Record

前端 未结 9 2564
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-15 13:12

Using Yii, how can I get an array from an Active Record.

Say something like this:

array(\'foo\', \'bar\', \'lala\')

From something like thi

9条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-15 13:35

    If i understand you correctly:

    $users = User::model()->findAll();
    $usersArr = CHtml::listData( $users, 'id' , 'name');
    print_r( $usersArr );
    

    It will give you array id => name

    Array {
        2 => 'someone',
        20 => 'kitty',
        102 => 'Marian',
        // ...
    }
    

提交回复
热议问题