[Help] Eval function with two dimention array !!

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
unisabilly
New php-forum User
New php-forum User
Posts: 19
Joined: Wed Aug 17, 2005 6:00 am

Wed Aug 17, 2005 6:17 am

Hi there ~
I have a question about eval function, i'm trying to read a tpl file line by line and convert the output to html, everything works fine except if the tpl contains two dimension array of variable my eval function fails to read the variable, following is an example :


Code: Select all

$temp["name"]["first"] = "Michael";
$temp["name"]["surname"] = "Lee";
$compile_line ='my name is $temp[name][first]';
[color=red]eval("\$compile_line=\"$compile_line\";");[/color]
echo $compile_line;


the eval function here can work with any variables even with array but not with two dimention array !!
and please if you can explain how the eval function works, coz i just know the above eval function can read the variable out of a line, but does not know how it works ~
Thanx ~ :D

User avatar
WiZARD
Moderator
Moderator
Posts: 1240
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

Wed Aug 17, 2005 7:44 am

$compile_line ='my name is ' . $temp['name']['first'];
try this. :)
"Sex,Drugs and Rock&Roll " replaced at "Sucks,Bugs and Plug&Play";
Image

unisabilly
New php-forum User
New php-forum User
Posts: 19
Joined: Wed Aug 17, 2005 6:00 am

Wed Aug 17, 2005 8:18 am

WiZARD wrote:$compile_line ='my name is ' . $temp['name']['first'];
try this. :)


sorry i didnt make it clear, the example might be just a line from the tpl files
which means " my name is ... " is just a part of the tpl file,

for example the tpl might look like following:

Code: Select all

<table>
<tr>
<td> my name is $temp['name']['first'] </td>
<td> i'm $temp['name']['age'] </td>
</tr>
</table>


the php file might look like following :

Code: Select all

.
.
.
$temp['name']['first'] = 'Michael';
$temp['name']['age'] = '20';
$original_file_array= file("test.tpl");
$file_size = sizeof($original_file_array);
$structure_counter = 0;

while($file_size > $structure_counter)
{
        $compile_line = $original_file_array[$structure_counter];
        eval("\$compile_line="$compile_line";");
        echo $compile_line;       
        $structure_counter++;
}


hope this can demonstrate the question more clearly :D

User avatar
WiZARD
Moderator
Moderator
Posts: 1240
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

Wed Aug 17, 2005 10:48 am

unisabilly wrote:
WiZARD wrote:$compile_line ='my name is ' . $temp['name']['first'];
try this. :)


sorry i didnt make it clear, the example might be just a line from the tpl files
which means " my name is ... " is just a part of the tpl file,

for example the tpl might look like following:

Code: Select all

<table>
<tr>
<td> my name is $temp['name']['first'] </td>
<td> i'm $temp['name']['age'] </td>
</tr>
</table>


the php file might look like following :

Code: Select all

.
.
.
$temp['name']['first'] = 'Michael';
$temp['name']['age'] = '20';
$original_file_array= file("test.tpl");
$file_size = sizeof($original_file_array);
$structure_counter = 0;

while($file_size > $structure_counter)
{
        $compile_line = $original_file_array[$structure_counter];
        eval("\$compile_line="$compile_line";");
        echo $compile_line;       
        $structure_counter++;
}


hope this can demonstrate the question more clearly :D

Ouch..... sorry.... now understand.....
I can propose to you only one thing
use each then you parsing array. eval not parsed associative array..... :o(
"Sex,Drugs and Rock&Roll " replaced at "Sucks,Bugs and Plug&Play";
Image

Post Reply