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

Link to comment
Share on other sites

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
Share on other sites

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

 

Noope papaface. The output will be just 'foo' - the name of the array.

Link to comment
Share on other sites

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
Share on other sites

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.