mayfair Posted May 29, 2010 Share Posted May 29, 2010 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 Link to comment https://forums.phpfreaks.com/topic/203273-concatenating-strings-to-form-the-name-of-a-variable/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 29, 2010 Share Posted May 29, 2010 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] ?> Link to comment https://forums.phpfreaks.com/topic/203273-concatenating-strings-to-form-the-name-of-a-variable/#findComment-1065007 Share on other sites More sharing options...
mayfair Posted May 29, 2010 Author Share Posted May 29, 2010 Thank you PFMaBiSmAd, this worked great. Link to comment https://forums.phpfreaks.com/topic/203273-concatenating-strings-to-form-the-name-of-a-variable/#findComment-1065012 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.