Page 1 of 1

eval problem

Posted: Thu Aug 10, 2006 10:50 am
by ablock
Hi,
The following code intends to cycle through a two dimensional representation of a database recordset and create an array for every column with the array name being the name of the column...

Code: Select all

foreach ($this->results as $row_n => $row) {
	   foreach ($row as $column => $value) {
		$evalcode =  '$this->' . strtolower($column) . "[$row_n]" . " = '$value';";
		eval($evalcode);
	  }
	}
this gives me the error: Parse error: parse error, unexpected T_STRING in .... : eval()'d code on line 1

this code however works:

Code: Select all

$column = "hello";
$row_n = "3";
$value = "t";
$evalcode =  '$this->' . strtolower($column) . "[$row_n]" . " = '$value';";
eval($evalcode);
am I missing something? Do you need more information to answer my question?

Posted: Thu Aug 10, 2006 10:58 am
by volka
No need for eval() here

Code: Select all

<?php
class foo {
	function bar() {
		$column = 'abc';
		$this->{$column} = 123;
	}
}

$f = new foo;
$f->bar();
echo $f->abc;
?>