2

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?

phpdev
  • 511
  • 4
  • 22

4 Answers4

4

Just set the proper value right from the beginning:

foreach ($data as $key => $value){
    $array_o[$key] = $value['region_id'];
}
Bizley
  • 17,392
  • 5
  • 49
  • 59
2

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.

venoel
  • 463
  • 3
  • 13
1

Try to make it like this

 foreach($data as $key=>$value){
        $array_o[$key] = $value['region_id'];
    }
Bara' ayyash
  • 1,913
  • 1
  • 15
  • 25
1

In your foreach do this:

 $array_o[$key] = $value['region_id'];
halfer
  • 19,824
  • 17
  • 99
  • 186
Mubashar Abbas
  • 5,536
  • 4
  • 38
  • 49