Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Variable variable OK! But with ARRAY indexes - problem!

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Variable variable OK! But with ARRAY indexes - problem!

Postby muTual » Thu Oct 17, 2002 1:12 am

It would be very useful for me if I could get values of multidimensional array using:
- name of array from string variable
- sequence of indexes of array from string variable
Code: Select all
$arr[1][2]="correct array value";

$arr_name="arr";
$arr_idx="[1][2]";

Next code works, but it's not I want get:
Code: Select all
echo 'example 1 = ('.${$arr_name}[1][2].')';

What I want is:
Code: Select all
// it doesn't return value:
echo 'example 2 = ('.${$arr_name}[$arr_idx].')';
// it doesn't return value too:
echo 'example 3 = ('.${$arr_name.$arr_idx}.')';

Somebody knows how to achieve effect returning correct value? I need help.
Thanks.
muTual
New php-forum User
New php-forum User
 
Posts: 3
Joined: Thu Oct 17, 2002 12:41 am
Location: Poland

Postby DoppyNL » Thu Oct 17, 2002 1:28 am

If you want to acces a variable with the name wich is stored in another variable, you will have to do this like this:
Code: Select all
$varname = 'variable_name';
$variable_name = 'Greetings Eartlings';
print($$varname);
this will output:
Greetings Earthlings
This is no different when using arrays or not.



When you want to use a variable to acces a specific index in you're array you can do that as follows:
Code: Select all
$my_array[12] = 'Greetings Eartlings';
$array_index = 12;
print($my_array[$array_index]);
this wil output:
Greetings Eartlings
This can be done with strings as well, works the same.


What you can't do is using 1 variable to acces a multi-dymension array:
Code: Select all
$my_array[12][15] = 'Greetings Eartlings';
$my_index = "[12][15]"
print($my_array[$my_index]); //-- or any variant
will not work!
You will have to use 2 variables to acces both indexes seperately.
Like this:
Code: Select all
$my_array[12][15] = 'Greetings Eartlings';
$my_index1 = 12;
$my_index2 = 15;
print($my_array[$my_index1][$my_index2];
will output:
Greetings Eartlings


Hope this helps.

Greetz Daan
DoppyNL
 

Postby muTual » Thu Oct 17, 2002 1:53 am

Thx for answer. I know that your examples work, but my $arr_idx variable is not always "two-dimension", it may be:
Code: Select all
$arr_idx="[1][2]";
//or
$arr_idx="[3]";
//or
$arr_idx="[2][3][1]";
//and so on...

$arr_idx is depended on my previous source code.
I'm curious if php compiler can interpret (in some way) array name jointly with indexes string...
Can I do this in some other way?
Thanks for other answers in advance.

Michael
muTual
New php-forum User
New php-forum User
 
Posts: 3
Joined: Thu Oct 17, 2002 12:41 am
Location: Poland

Postby DoppyNL » Thu Oct 17, 2002 2:02 am

As far as I know you can't acces a multi-level array like that (but I could be wrong).

a possible solution could be this:
Code: Select all
$your_array_name[1][3][12] = 'Greetings Earthlings';
$indexes = '[1][3][12]';
$tempvar = 'print($your_array_name' . $indexes . ');';
eval($tempvar);
$tempvar equals to this:
print($your_array_name[1][3][12]);
and the output is this:
Greetings Earthlings


It's not very nice coding, but it works.
I think there is a better way to do this, only I don't know it.
Perhaps someone else?

Greetz Daan
DoppyNL
 

Postby muTual » Thu Oct 17, 2002 2:54 am

Yes it works! That's the point! That's what I was looking for!

I'm php coder for some time but I didn't know about eval() function in php. I'm so embarrassed. To punish myself I have to read PHP Documentation once again (and rewrite it!) :wink:

I'm interested too if there is some other solution.

Sorry for disturbance. I'm very grateful for help.
muTual
New php-forum User
New php-forum User
 
Posts: 3
Joined: Thu Oct 17, 2002 12:41 am
Location: Poland

Postby Jay » Thu Oct 17, 2002 3:30 am

For the way you want to do it, the best solution is the eval() one!
Jay
 


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.