Jump to content

Concatenating strings to form the name of a variable


mayfair

Recommended Posts

Hello guys,

 

I hope I can explain this clearly. I have a number of variables that are created inside a loop containing results from mysql_fetch_array:

 

for ($i=1; $i<=7; $i++) {
$row = mysql_fetch_array($result);

$EUR[$i] = $row['EUR'];
$USD[$i] = $row['USD'];
$AUD[$i] = $row['AUD'];
$NZD[$i] = $row['NZD'];
$CAD[$i] = $row['CAD'];
}

 

So I can now call any particular value by echoing say "$EUR[2]" or "$USD[5]".

 

Next I have two variables that are created using $_GET - $currency - which can be "EUR", "USD", "AUD" etc. and $delivery_period - which can be 1, 2, 3 etc.

 

What I would like to be able to do is concatenate $currency and $delivery_period to form $EUR[3] or $USD[1] etc. so I can actually echo the value in the corresponding array on-screen. My PHP skills aren't that strong and I was told the solution maybe used eval() but I can't get anything to work. Any help would be greatly appreciated :)

Just use one array -

<?php
$arr = array();
for ($i=1; $i<=7; $i++) {
$row = mysql_fetch_array($result);
$arr['EUR'][$i] = $row['EUR'];
$arr['USD'][$i] = $row['USD'];
$arr['AUD'][$i] = $row['AUD'];
$arr['NZD'][$i] = $row['NZD'];
$arr['CAD'][$i] = $row['CAD'];
}

$currency = 'EUR';
$delivery_period  = 3;

echo $arr[$currency][$delivery_period]; // $arr['EUR'][3]
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.