ATLChris Posted September 24, 2010 Share Posted September 24, 2010 OK, what I am trying to do is generate a dynamic variable name. I want to setup a foreach statement that will generate variable names based on an array of data. $numbers = array('one', 'two', 'three', 'four', 'five', 'six', 'seven'); foreach ($numbers as $number) { } What I need, is for a bunch of variables to be generated and filled with content. $variable_one_label = "Blah"; $variable_two_label = "Blah Again"; How can I generate the dynamic variable based on the array value? Quote Link to comment https://forums.phpfreaks.com/topic/214290-help-generating-a-dynamic-variable-name/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 24, 2010 Share Posted September 24, 2010 Just use an array (arrays are for sets/series of related data values.) Quote Link to comment https://forums.phpfreaks.com/topic/214290-help-generating-a-dynamic-variable-name/#findComment-1115107 Share on other sites More sharing options...
ATLChris Posted September 24, 2010 Author Share Posted September 24, 2010 Just use an array (arrays are for sets/series of related data values.) I am not sure I follow. Can you give me an example of what you mean? Quote Link to comment https://forums.phpfreaks.com/topic/214290-help-generating-a-dynamic-variable-name/#findComment-1115109 Share on other sites More sharing options...
PFMaBiSmAd Posted September 24, 2010 Share Posted September 24, 2010 Based on the information you provided in the first post - $array = array(); $array['one'] = "Blah"; $array['two'] = "Blah Again"; If you provide more complete information about where and what you are getting or setting values, someone could provide a more complete example. Quote Link to comment https://forums.phpfreaks.com/topic/214290-help-generating-a-dynamic-variable-name/#findComment-1115112 Share on other sites More sharing options...
JonnoTheDev Posted September 24, 2010 Share Posted September 24, 2010 Why would you want to create variable variables from an array of data. The data can be accessed directly from the array; $numbers = array('one', 'two', 'three', 'four', 'five', 'six', 'seven'); // prints one print $numbers[0]."<br />"; // prints two print $numbers[1]; Or as PFMaBiSmAd has suggested use an associative array if you want to allocate data to custom keys. $numbers = array('one' => 'blah', 'two' => 'blah', 'three' => 'blah', 'four' => 'blah', 'five' => 'blah', 'six' => 'blah', 'seven' => 'blah'); foreach($numbers as $number => $value) { print $number.": ".$value."<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/214290-help-generating-a-dynamic-variable-name/#findComment-1115114 Share on other sites More sharing options...
ATLChris Posted September 24, 2010 Author Share Posted September 24, 2010 Based on the information you provided in the first post - $array = array(); $array['one'] = "Blah"; $array['two'] = "Blah Again"; If you provide more complete information about where and what you are getting or setting values, someone could provide a more complete example. Why would you want to create variable variables from an array of data. The data can be accessed directly from the array; $numbers = array('one', 'two', 'three', 'four', 'five', 'six', 'seven'); // prints one print $numbers[0]."<br />"; // prints two print $numbers[1]; Or as PFMaBiSmAd has suggested use an associative array if you want to allocate data to custom keys. The only problem with that is I need to get 3 different variables for each array option. i.e. $label_one, $type_one, $validation_one, ... Quote Link to comment https://forums.phpfreaks.com/topic/214290-help-generating-a-dynamic-variable-name/#findComment-1115119 Share on other sites More sharing options...
kenrbnsn Posted September 24, 2010 Share Posted September 24, 2010 User three arrays: <?php $labels = array(); $types = array(); $validations = array(); $numbers = array('one', 'two', 'three', 'four', 'five', 'six', 'seven'); foreach ($numbers as $n) { $labels[$n] = 'blah'; $validations[$n] = 'other stuff'; $types[$n] = 'more stuff'; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/214290-help-generating-a-dynamic-variable-name/#findComment-1115125 Share on other sites More sharing options...
JonnoTheDev Posted September 24, 2010 Share Posted September 24, 2010 Or a multi-dimensional associative array (not as complex as it sounds) $numbers = array( 'one' => array('var1' => 'blah 1', 'var2' => 'blah 2'), 'two' => array('var1' => 'blah 3', 'var2' => 'blah 4')); // will print blah 1 print $numbers['one']['var1']."<br />"; // will print blah 4 print $numbers['two']['var2']."<br />"; As the above post states you can add data to arrays within loops Quote Link to comment https://forums.phpfreaks.com/topic/214290-help-generating-a-dynamic-variable-name/#findComment-1115145 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.