Activeform Checkboxlist Yields Array Keys


I am putting an array in the $data field of the activeForm checkBoxList.

When saved, the values of the array are not saved instead it is saved as the array keys.

How should I extract the array values instead?

Below is the code

$names is an array of data from the DB

echo $form->checkBoxList($model,'name', $names


// when model is saved, $name = 0,1,2,3 instead of the actual name

I even tried the following

echo $form->checkBoxList($model,'name', array_values($names)


// when model is saved, $name = 0,1,2,3 instead of the actual name

It yielded the same result.

I’ve checked the integrity of the array by printing out values of $name i.e.

echo $name[1]; //Yielded 'John'

Seems to be intact. Not sure how to solve this now.

How do you save it?

Saved it like this

public function actionCreate()


		$model = new Letter;

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);






			$model->name = implode(",",$model->name);




					$this->redirect(array( '/prf/activeCam', ));  







I see.

Since checkboxList requires the array of ‘value’ => ‘label’ as options, all you need to do is to create appropriate array for $names.

You can use array_combine for that.

NICE!! it works!! thanks :D

Here’s how its solved

//Swopped Array Keys to its values.

$nameKeys = array_values($names);

$combineNames = array_combine($names, $nameKeys);

//Pass this into checkBoxList

echo $form->checkBoxList($model,'name',$combineNames);


Thanks again man!!

I suppose you can do array_combine($names, $names); since $names is probably just an array of strings

oh yeah! that’s right HA!

boggled mind in procedural mode