Jump to content

creating an array list to become $variables

Go to solution Solved by requinix,

Recommended Posts

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?

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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, ...

Link to comment
Share on other sites

@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 by phppup
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.