iwato Posted March 19, 2010 Share Posted March 19, 2010 PROBLEM: I have an array of undetermined size whose elements I wish to split and assign each to a separate sequentially numbered list of variables. How does one dynamically create a sequential list of variables.The output should appear as follows:element1, element2, ... , elementnwhere n can be any finite number.I have tried the following, but it fails to recognize the concatenation as a new variable name and results in an infinite loop of error messages. <?php $i = 1; $n = 6; while ($i < 6) { $element . $i = $i . "<br />"; echo $element . $i; $i++; }?> Roddy Link to comment Share on other sites More sharing options...
ShadowMage Posted March 19, 2010 Share Posted March 19, 2010 You can use variables to declare variable names, like this: <?php $i = 1; $n = 6; while ($i < $n) { $varName = 'element'.$i; $$varName = $i . "<br />"; echo '$varName: '.$$varName; $i++; } //Then you can call it by it's real name, too echo "element2: ".$element2."<br />";?> Link to comment Share on other sites More sharing options...
iwato Posted March 20, 2010 Author Share Posted March 20, 2010 <?php $i = 1; $n = 6; while ($i < $n) { $varName = 'element'.$i; $$varName = $i; echo '$varName: '.$$varName . "<br />"; $i++; } echo "The value of $element3 is " . $element3 . "."; ?> Thanks, jkloth. I made some minor changes to your code, so that it would better reflect what I believe you meant it to say, and it works fine.1) What I find amazing about this code is the double use of the dollar sign. Does this mean that you can make the value of any variable into a variable name by simply doubling the $ sign of the variable name and initializing the new result?2) Also, I am very surprised that the presence of the dollar sign inside a quoted string retains its character of a variable. Notice that I had to insert the special character $#36; in order to get the phrase $element3 to appear as text. How might one explain this phenomenon?PHP has many surprises!Roddy Link to comment Share on other sites More sharing options...
boen_robot Posted March 20, 2010 Share Posted March 20, 2010 If all variables are related, it's probably a better idea to use an array. An array can very easily be manipulated and traversed, whereas for using variable variables, you need to be much more clever in mass manipulation of all values. Link to comment Share on other sites More sharing options...
iwato Posted March 20, 2010 Author Share Posted March 20, 2010 If all variables are related, it's probably a better idea to use an array. An array can very easily be manipulated and traversed, whereas for using variable variables, you need to be much more clever in mass manipulation of all values.Unfortunately, the elements were of a mixed bag. The goal was to extract a comma delimited line from a larger body of text and parse the various elements of the selected line. This is what worked thanks to everyone's input. Of course, there may be a better way to do it of which I am not aware. Still, I am very happy! Hooray! Hooray! <?php $i = 1; $n = 6; $file = fopen("mixed.txt", "r"); while ($i < $n) { $varName = 'element' . $i; $$varName = fgetcsv($file); $i++; } echo count($element3); foreach ($element3 as $greeting) { echo $greeting . "<br />"; } ?> Roddy Link to comment Share on other sites More sharing options...
boen_robot Posted March 20, 2010 Share Posted March 20, 2010 The variables are still related - processing them in the same fashion makes them related. Therefore, arrays are a better solution here. <?php $element = array(); $file = fopen("mixed.txt", "r"); for ($i=0; $i < 6; $i++) { $element[] = fgetcsv($file); } echo count($element[3]); foreach ($element[3] as $greeting) { echo $greeting . "<br />"; } ?> If you want to expand this to echo the contents of all values in the $element array, you can easily do so with something like: <?php $element = array(); $file = fopen("mixed.txt", "r"); for ($i=0; $i < 6; $i++) { $element[] = fgetcsv($file); } foreach($element as $csvElement) { echo count($csvElement); foreach ($csvElement as $greeting) { echo $greeting . "<br />"; } } ?> This kind of expansion would meant a lot more code if you used variable variables. Link to comment Share on other sites More sharing options...
iwato Posted March 22, 2010 Author Share Posted March 22, 2010 <?php $element = array(); $file = fopen("mixed.txt", "r"); for ($i=0; $i < 6; $i++) { $element[] = fgetcsv($file); } echo count($element[3]); foreach ($element[3] as $greeting) { echo $greeting . "<br />"; } ?> <?php $element = array(); $file = fopen("mixed.txt", "r"); for ($i=0; $i < 6; $i++) { $element[] = fgetcsv($file); } foreach($element as $csvElement) { echo count($csvElement); foreach ($csvElement as $greeting) { echo $greeting . "<br />"; } } ?> Yes, these are slick improvements. I tried both with minor modification, and they produced very nice results. Still, it was important to learn how to generate a sequentially numbered variable list. Many thanks!Roddy Link to comment Share on other sites More sharing options...
ShadowMage Posted March 22, 2010 Share Posted March 22, 2010 2) Also, I am very surprised that the presence of the dollar sign inside a quoted string retains its character of a variable. Notice that I had to insert the special character $#36; in order to get the phrase $element3 to appear as text. How might one explain this phenomenon?To explain this "phenomenon" there are two type of quotes used by PHP: single (') and double (") quotes.Single quotes will print exactly what is within them. For example:$test = "a test"$singleQuote = 'This is $test';echo $singleQuote; The above will print out this -> This is $testDouble quotes will interpolate variable names into their values so the following: $test = "a test"$doubleQuote = "This is $test";echo $doubleQuote; will print this -> This is a test Link to comment Share on other sites More sharing options...
iwato Posted March 22, 2010 Author Share Posted March 22, 2010 To explain this "phenomenon" there are two type of quotes used by PHP: single (') and double (") quotes.I noticed a question related to the use of quotation marks on the W3Schools PHP exam and could not answer it. This is when I realized that I needed to study more. I have tried both of your examples and got them to reproduce your output.Thanks, I am very happy that this forum is here. It makes self-study so much easier when knowledgeable people are around to answer questions.Roddy Link to comment Share on other sites More sharing options...
Synook Posted March 23, 2010 Share Posted March 23, 2010 Note that if you use double quotes and want to insert a special character literally, you can just escape it with the backslash character: echo "\$this"; //will display "$this" Link to comment Share on other sites More sharing options...
iwato Posted March 24, 2010 Author Share Posted March 24, 2010 Note that if you use double quotes and want to insert a special character literally, you can just escape it with the backslash character:echo "\$this"; //will display "$this" Yeah, the backslash is a handy escape character, but does it only work with double quotes?Roddy Link to comment Share on other sites More sharing options...
boen_robot Posted March 24, 2010 Share Posted March 24, 2010 Yeah, the backslash is a handy escape character, but does it only work with double quotes?RoddyIt works on single quotes too, but it works on less characters. See the PHP manual on strings for details. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.