Jump to content

Help Generating a Dynamic Variable Name


ATLChris

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/214290-help-generating-a-dynamic-variable-name/
Share on other sites

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.

$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 />";
}

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

 

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

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

Archived

This topic is now archived and is closed to further replies.

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