pault Posted December 2, 2011 Share Posted December 2, 2011 This is difficult to explain but here goes. Imagine I have several variables called $field1, $field2, $field3 (not the best way to store things I know but that's the way it is - it's actually to do with columns in a MySQL database) and I want to loop through them and put them into an array. Ideally I'd want something like this: for ($j = 1; $j < 4; ++$j) { $arrayvar[$j] = eval('$field' . $j); } so that the loop counter was added to the word $field to create $field1, $field2 and each line became the equivalent of $arrayvar[1]=$field1, thus automating the procedure. Eval works but you have to do it on the whole line: eval('$arrayvar'.[$j]. '=$field'.[$j].';'); So is there any way of using a sort of eval for just part of a line? Hope you understand and thanks for any advice. Quote Link to comment https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/ Share on other sites More sharing options...
kickstart Posted December 2, 2011 Share Posted December 2, 2011 Hi Think what you want is something like this:- http://php.net/manual/en/language.variables.variable.php Not tested but something like this:- <?php for ($j = 1; $j < 4; ++$j) { $fieldname = $field.$j; $arrayvar[$j] = $$fieldname; } ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293438 Share on other sites More sharing options...
pault Posted December 2, 2011 Author Share Posted December 2, 2011 That didn't quite work but for ($j = 1; $j <= 4; ++$j) { $fieldname = 'field' . $j; $arrayvar[$j] = $$fieldname; } did. Thanks for putting me on the right track. Quote Link to comment https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293442 Share on other sites More sharing options...
PFMaBiSmAd Posted December 2, 2011 Share Posted December 2, 2011 How are your original $field1, $field2, $field3,.. variables getting set? Having code that sets them, then having more code to put them into an array is doubling the amount of code, processing time, and memory needed. It's often easily possible to directly get them into an array when they are created. Quote Link to comment https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293448 Share on other sites More sharing options...
pault Posted December 2, 2011 Author Share Posted December 2, 2011 They are actually columns in a MySQl database. It is a 'globals' table and originally there were only two so I called them code1 and code2. Now they have grown in number and it would be better to separate them into a different table but I can't face all the re-writing at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293476 Share on other sites More sharing options...
PFMaBiSmAd Posted December 2, 2011 Share Posted December 2, 2011 If these are columns in a database table, when you fetch them they are ALREADY in an array. Why have lines of code that assign them to a series of scaler php variables, then have more code to put them back into an array? Quote Link to comment https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293477 Share on other sites More sharing options...
pault Posted December 2, 2011 Author Share Posted December 2, 2011 You mean extract the data from the database using numerical keys instead of associative? Eg. $var[$j] = $row[$j+x]; sort of thing (where x is the number in the offset in the mysqli_fetch_row. Quote Link to comment https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293478 Share on other sites More sharing options...
kickstart Posted December 2, 2011 Share Posted December 2, 2011 Hi I agree with PFMaBiSmAd. If they are columns then nothing to stop you doing something like as follows:- <?php $j=1; while ($row = mysql_fetch_array($rs)) { $arrayvar[$j] = $row['field'.$j]; $j++; } ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293482 Share on other sites More sharing options...
PFMaBiSmAd Posted December 2, 2011 Share Posted December 2, 2011 Eg. $var[$j] = $row[$j+x]; Nop, that's still too much unnecessary code. What's wrong with the array: $row (or just use your final name when you fetch it, something like - $settings = mysql_fetch_assoc($result) At this point it, without seeing your query statement and what you are trying to get as an end result, cannot specifically help you more than to mention the concept and that every time we see someone with a series of numbered variables that their code is overly complex for what is being accomplished. Quote Link to comment https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293483 Share on other sites More sharing options...
pault Posted December 2, 2011 Author Share Posted December 2, 2011 I couldn't just use the whole returned row because there are other columns as well as the numbered ones (forgot to mention that) so I would have to filter them out. But your answers have given me enough to sort the problem out and I've learnt a few things today. Thanks to you both. Quote Link to comment https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293488 Share on other sites More sharing options...
PFMaBiSmAd Posted December 2, 2011 Share Posted December 2, 2011 If the point of this specific code is to get something like global settings (maybe someones multipliers in a RPG...), your query should just retrieve the columns you want and you would be done. In programming, the best method of accomplishing something generally takes knowing what you are actually doing, which is why just showing a few lines of your code out of context rarely results in the best solution. I'm not trying to drag this out to give you a hard time. If what you are trying to create is a web application that will some day need to perform its best to handle a large number of visitors at the same time, having line after line of code that does something to data, only to be followed by more code that undoes what was just done, is something you want to nip in the bud as soon as possible in your coding. Quote Link to comment https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293492 Share on other sites More sharing options...
kickstart Posted December 2, 2011 Share Posted December 2, 2011 Hi Just realised the code I pasted earlier was garbage and might have confused you. Something more like this:- <?php $j=1; if ($row = mysql_fetch_array($rs)) { for($j = 1;$j<=10;$j++) { $arrayvar[$j] = $row['field'.$j]; } } ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293498 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.