diamondnular Posted July 7, 2008 Share Posted July 7, 2008 Hi folks, I am working with a function which will return an array whose name can be entered by the user. Basically, the code is: function test() { // get the array name $array_name = 'foo'; // initiate all elements to be zeros for ( $i=1;$i<=10;$i++ ) { ${$array_name}[$i] = ''; } // assign each element of array a value for ( $i=1;$i<=10;$i++ ) { ${$array_name}[$i] = 'bar' . $i; } return ${$array_name}; } Now in order to use the return array, I need to assign that array to a new array, and... I do not know how to do that. The code $new_array = test(); does not work, it always gives me errors. Anybody helps please! Thanks a bunch. D. Link to comment https://forums.phpfreaks.com/topic/113644-solved-how-to-return-variable-variable/ Share on other sites More sharing options...
papaface Posted July 7, 2008 Share Posted July 7, 2008 <?php function test() { // get the array name $array_name = 'foo'; // initiate all elements to be zeros for ( $i=1;$i<=10;$i++ ) { $$array_name[$i] = ''; } // assign each element of array a value for ( $i=1;$i<=10;$i++ ) { $$array_name[$i] = 'bar' . $i; } return $array_name; } $test = test(); print_r($test); ?> Do you mean like that? Link to comment https://forums.phpfreaks.com/topic/113644-solved-how-to-return-variable-variable/#findComment-584004 Share on other sites More sharing options...
br0ken Posted July 7, 2008 Share Posted July 7, 2008 I'm not too sure about arrays as I rarely use them but if you can't find a way to do this you could always loop through your array in the function storing each value in a string seperated by a comma. Return this string and then using the explode() function, create an array. <?php $arr = explode(",", test()); ?> Link to comment https://forums.phpfreaks.com/topic/113644-solved-how-to-return-variable-variable/#findComment-584006 Share on other sites More sharing options...
roopurt18 Posted July 7, 2008 Share Posted July 7, 2008 The original code you posted looks ok to me. What were the errors you received? I ran your code and it works fine: <?php function test() { // get the array name $array_name = 'foo'; // initiate all elements to be zeros for ( $i=1;$i<=10;$i++ ) { ${$array_name}[$i] = ''; } // assign each element of array a value for ( $i=1;$i<=10;$i++ ) { ${$array_name}[$i] = 'bar' . $i; } return ${$array_name}; } $new_array = test(); print_r($new_array); ?> Link to comment https://forums.phpfreaks.com/topic/113644-solved-how-to-return-variable-variable/#findComment-584031 Share on other sites More sharing options...
diamondnular Posted July 7, 2008 Author Share Posted July 7, 2008 <?php function test() { // get the array name $array_name = 'foo'; // initiate all elements to be zeros for ( $i=1;$i<=10;$i++ ) { $$array_name[$i] = ''; } // assign each element of array a value for ( $i=1;$i<=10;$i++ ) { $$array_name[$i] = 'bar' . $i; } return $array_name; } $test = test(); print_r($test); ?> Do you mean like that? Noope papaface. The output will be just 'foo' - the name of the array. Link to comment https://forums.phpfreaks.com/topic/113644-solved-how-to-return-variable-variable/#findComment-584037 Share on other sites More sharing options...
diamondnular Posted July 7, 2008 Author Share Posted July 7, 2008 The original code you posted looks ok to me. What were the errors you received? I ran your code and it works fine: <?php function test() { // get the array name $array_name = 'foo'; // initiate all elements to be zeros for ( $i=1;$i<=10;$i++ ) { ${$array_name}[$i] = ''; } // assign each element of array a value for ( $i=1;$i<=10;$i++ ) { ${$array_name}[$i] = 'bar' . $i; } return ${$array_name}; } $new_array = test(); print_r($new_array); ?> Uhm, interesting. I just tried to run with just the php file alone, and it works too! The problem arose when I tried to modify some codes in a module in Joomla 1.5.3, and with the similar code above, my server returns error: [Mon Jul 07 19:15:33 2008] [error] [client 127.0.0.1] PHP Catchable fatal error: Object of class stdClass could not be converted to string in .../default.php on line 12, referer: http://127.0.0.1/joomla/ Maybe this is some kind of restrictions in Joomla engine, not the problem of the php code. Link to comment https://forums.phpfreaks.com/topic/113644-solved-how-to-return-variable-variable/#findComment-584052 Share on other sites More sharing options...
diamondnular Posted July 8, 2008 Author Share Posted July 8, 2008 Got it sorted now . I made a typo when typing in the codes, and it seemed taking me forever to find it without your helps. Thanks for prompt helps, guys D. Link to comment https://forums.phpfreaks.com/topic/113644-solved-how-to-return-variable-variable/#findComment-584100 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.