Jump to content

[SOLVED] how to return variable variable?


diamondnular

Recommended Posts

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

<?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?

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());

?>

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);
?>

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

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.

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.