- This topic is empty.
-
AuthorPosts
-
May 8, 2010 at 8:24 am #28961smerdonwMember
I’m looking for a way to easily loop through the elements of an Array and create individual String variables. I’ve messed around a bit with for and foreach and can get the loops to display the elements but really need to incrementally save them into strings so I can operate on them later. I can’t use a function as the environment I will be using this script does not support them. I would appreciate any help.
May 11, 2010 at 4:29 pm #75360HarZensMemberHere’s something I made for you, I hope you can understand it with the comments I left!
Code:# The array with data
$array = array( ‘value 1’, ‘value 2’, ‘value 3’, ‘value 4’, ‘value 5’ );
# Let’s print out what the array has and how
var_dump($array);
# Run over each item
foreach($array as $key => $value)
{
# We will store the new variable’s name in this variable
# Notice how I’m using the key from the array’s element to create a dynamic name for the “individual variable”
$var_name = ‘variable_’.$key;
# Here, I’m using one of the features that PHP has for variables. And it is that you can actually name a variable with a variable.
# In the first run, the Key value will be 0, so the $var_name will be “variable_0″. On the line below, I’m assigning the value from $value to $$var_name.
# But here is the thing, PHP will first interpret $var_name, making it look like $variable_0 and then assign the $value to that dynamically named variable.
$$var_name = $value;
# In this line I’m just storing the NEW variables names so I can use them later
$new_variables_list[] = $var_name;
}
# We could print the list of new variables just to see what happened
var_dump($new_variables_list);
# In this foreach I’m using the saved list to iterate on the new variables and see if it worked, and if it did, what name/values they have.
foreach($new_variables_list as $nvl)
echo ‘$’.$nvl.‘ = “‘.$$nvl.‘”;<br />’;
# Now go have fun :DOn the other hand, and out of curiosity, why would you create individual variables instead of using the array itself?
Instead of calling it using "$new_variable_name_0", you could simply use "$original_array[0]"… But I’m curious, there should be a reason for it, I guess.
Hope it helped you!
Cheers,
HarZensMay 11, 2010 at 8:11 pm #75379smerdonwMemberThanks! I will take a little time to digest it but it looks good. I using an online web tool which allow me to embed PHP and I needed a script that I can use to grab the values from the results of an answer and put them into individual variables so I could include them in a resulting email. Since the answers value will vary in quantity, I needed to run through what was there and set the value of a hidden question for each and then I could include them in a email.
May 12, 2010 at 9:22 pm #75486smerdonwMemberHey HarZens,
Is there a long way to implement the $$var_name = $value; statement? Unfortunately within the environment available to me, I can’t use all of PHP’s features.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.