phppup Posted August 3, 2022 Share Posted August 3, 2022 I wanted to take a list of table headings and use the identical titles as variables (to save time ... LOL) Tried this $list = array('fname', 'lname', 'email'); foreach($list as $val){ $new = "$".$val; echo $new; //see if this is working visually $newArray[] = $new; } print_r($new); //see if this is working visually And while it LOOKS as if it was accomplished, the values are NOT functioning as variables, BUT rather as values that begin with a dollar sign. How can I create $fname, $lname, $email and make them function as variables without physically repeating the keystroke process? Quote Link to comment https://forums.phpfreaks.com/topic/315131-creating-an-array-list-to-become-variables/ Share on other sites More sharing options...
mac_gyver Posted August 3, 2022 Share Posted August 3, 2022 there's no good reason to create a bunch of discrete variables for a set of data. just keep the data as a set, in an array variable. arrays are for sets of things where you will operate on each member of the set in the same/similar way. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315131-creating-an-array-list-to-become-variables/#findComment-1598914 Share on other sites More sharing options...
ginerjm Posted August 3, 2022 Share Posted August 3, 2022 (edited) Totally agree with macgyver. I wonder if the OP can give us a succinct description why he/she thinks this is something worth doing? OP - that last test with the printr call is not going to do anything for you. Look at your code again. PPS - Those titles are already variables in case you want to know. They are $list[0], $list[1] & $list[2]. Edited August 3, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/315131-creating-an-array-list-to-become-variables/#findComment-1598915 Share on other sites More sharing options...
Solution requinix Posted August 3, 2022 Solution Share Posted August 3, 2022 PSA: I will remove any further posts which tell OP how to do the task as-asked. As well-intentioned as the question and its direct answer is, using variable variables is not a good thing. Quote Link to comment https://forums.phpfreaks.com/topic/315131-creating-an-array-list-to-become-variables/#findComment-1598918 Share on other sites More sharing options...
ginerjm Posted August 3, 2022 Share Posted August 3, 2022 I didn't get what that poster said anyway. Whatever did the word 'John' have to do with the OP's example? Beats me! Quote Link to comment https://forums.phpfreaks.com/topic/315131-creating-an-array-list-to-become-variables/#findComment-1598919 Share on other sites More sharing options...
phppup Posted August 3, 2022 Author Share Posted August 3, 2022 (edited) I've taken @mac_gyver advice and abandoned the effort. [PS: Did I miss a solution?? LOL] Edited August 3, 2022 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/315131-creating-an-array-list-to-become-variables/#findComment-1598926 Share on other sites More sharing options...
ginerjm Posted August 3, 2022 Share Posted August 3, 2022 Score one for the home team!! Quote Link to comment https://forums.phpfreaks.com/topic/315131-creating-an-array-list-to-become-variables/#findComment-1598927 Share on other sites More sharing options...
requinix Posted August 3, 2022 Share Posted August 3, 2022 1 hour ago, ginerjm said: I didn't get what that poster said anyway. Whatever did the word 'John' have to do with the OP's example? Beats me! Because "John" is a reasonable example of an fname, I would think... Quote Link to comment https://forums.phpfreaks.com/topic/315131-creating-an-array-list-to-become-variables/#findComment-1598928 Share on other sites More sharing options...
mac_gyver Posted August 3, 2022 Share Posted August 3, 2022 what you are trying to do is dynamically process data using a data-driven design, rather than to write out repeated code and variables for every possible field. if you search the forum for that phrase, you will find some examples. you need a data structure (array, db table) that holds a definition of all the dynamic values associated with each field in a set, such the field name, a human readable label, a type, validation rules, processing rules, multi choice/select values, ... you would then loop over the data in this defining structure and use simple, general-purpose code to operate on the actual data. i typically use the field name as the defining array's index values, since the field name/index must be unique. the label gets used when building error messages, as a label for form fields, as html table headings, e.g. what you mentioned, ... the type is used to control any different processing, via a switch/case statement. validation rules include required validation, format validation, value (range) validation, character class validation. processing rules control being used in an insert query, the set part of an update query, the where part of an update query, in the where part of a delete query, in an email body, in an email header, ... Quote Link to comment https://forums.phpfreaks.com/topic/315131-creating-an-array-list-to-become-variables/#findComment-1598929 Share on other sites More sharing options...
phppup Posted August 3, 2022 Author Share Posted August 3, 2022 (edited) @mac_gyver I appreciate the additional insight but I generally like to understand the code that I use. Thus, even if I were "handed" a solution, I wouldn't use it unless I would be comfortable with it. I thought my initial post would kick right in and function as planned, but SURPRISE... LOL Meanwhile, I've modified what I had and will manually cut & paste the result to save some keystrokes (unless I find some quick reference to enlighten me). Ironically, I've probably spent MORE keyboard pounding and time overload TRYING to make this work, than the potential time that the dynamic process would ever save. LOL But I suppose that is what makes this so much fun. Thanks to everyone that offered their advice and insight. Edited August 3, 2022 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/315131-creating-an-array-list-to-become-variables/#findComment-1598931 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.