phpdev
phpdev

Reputation: 511

arrays in foreach in yii 1

I have following code in my controller:

 $data= Yii::app()->db->createCommand()
                    ->select('region_id')
                    ->from('user_rights')
                    ->where('user_group_id='.$findRegion['user_group_id'])
                    ->queryAll();

 foreach($data as $key=>$value){
            $array_o[$key] = $value;
        }

var_dump($array_o); returns following value:

array(2) { [0]=> array(1) { ["region_id"]=> string(4) "1703" } [1]=> array(1) { ["region_id"]=> string(4) "1706" } }

But, I need to get similar to following value:

array(2) { [0]=> string(4) "1703" [1]=> string(4) "1706" }.

How can I do it?

Upvotes: 2

Views: 1148

Answers (4)

Mubashar Abbas
Mubashar Abbas

Reputation: 5663

In your foreach do this:

 $array_o[$key] = $value['region_id'];

Upvotes: 1

venoel
venoel

Reputation: 458

You can use queryColumn() method

So it is just enough set statment

$data= Yii::app()->db->createCommand()
                ->select('region_id')
                ->from('user_rights')
                ->where('user_group_id='.$findRegion['user_group_id'])
                ->queryColumn();

and delete your foreach statment.

Upvotes: 2

Bara' ayyash
Bara' ayyash

Reputation: 1935

Try to make it like this

 foreach($data as $key=>$value){
        $array_o[$key] = $value['region_id'];
    }

Upvotes: 1

Bizley
Bizley

Reputation: 18021

Just set the proper value right from the beginning:

foreach ($data as $key => $value){
    $array_o[$key] = $value['region_id'];
}

Upvotes: 4

Related Questions