Array ( [name] => Array ( [0] => 1 [1] => 1 ) [age] => Array ( [0] => today [1] => today ) [grp] => Array ( [0] => 2 [1] => 2 ) )
如何将其保存到yii中的表中。 name,age和grp可以有无限数量的值。
表ID,名称,年龄和grp
答案 0 :(得分:4)
如果每个数组索引都是数据库中的新记录,则可以在控制器中使用它:
$lenth = count($yourArray['name']); // this line will return the count of records must be inserted to db table
for( $i = 0 ; $i < $lenth ; $i++ )
{
$newRecord = new ModelName;
$newRecord->name = $yourArray['name'][$i];
$newRecord->age = $yourArray['age'][$i];
$newRecord->grp = $yourArray['grp'][$i];
$newRecord->save();
}