Search code examples
phpmysqlinsertkohana

Column name of the table as a variable in Kohana3 framework


I have such a code in the Kohana3 framework, that saves values in the Days table.

$column_names=array('morning','day','evening','night');
$values=array('abc','xyz','eeeee','ooooo');

$result=ORM::factory('Day');
$result->morning=$values[0];
$result->day=$values[1];
$result->evening=$values[2];
$result->night=$values[3];
$result->save();

How can I use $column_names to write something like, i.e. using variable as a column name:

$result->$column_names[3]=$values[3];

Solution

  • You can use the parentheses to wrap your array in.

    $result->{$column_names[3]} = $values[3];