Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

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