phpdev
phpdev

Reputation: 511

How to convert array to string in yii 1

I have following code in my controller in my yii 1application:

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

var_dump($data) returns following result:

array(2) { [0]=> array(1) { ["user_group_id"]=> string(1) "1" } [1]=> array(1) { ["user_group_id"]=> string(1) "3" } } 

How can I convert this array result into string

Upvotes: 0

Views: 1759

Answers (2)

Yupik
Yupik

Reputation: 5032

Simple display one per one:

foreach($data as $item) {
   echo $item['user_group_id'];
}

Fetch to one string (like @Ripper mentioned):

implode(',', array_column($data, 'user_group_id'));

It depends on what u want to do with this results. Please describe it so we can provide best solution.

Upvotes: 1

Ripper
Ripper

Reputation: 1162

Just use implode combined with array_column as

imlpode(',', array_column($data, 'user_group_id'));

Upvotes: 1

Related Questions