Solved

Variable Variables

Posted on 2004-04-24
5
208 Views
Last Modified: 2006-11-17
Basically, I have 5 arrays, and I want to pick which array to search based on a random number...so here is the layout:

$array1 = array("a","b","c");
$array2 = array("1","2","3");
$array3 = array("a","b","c");
$array4 = array("e","f","g");
$array5 = array("7","6","5");

$$randnumber = rand(1,5);

if(array_search($string,$array{$randnumber})) // if $randnumber is 2, use $array2 and etc...
{
...
}

However, it will not pick up what array to use, can somebody fix my code so I can use variable variables?
0
Comment
Question by:drakkarnoir
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:robinet02
Comment Utility
The easiest is to use an array of arrays instead of variable variables :

$array[1] = array("a","b","c");
$array[2] = array("1","2","3");
$array[3] = array("a","b","c");
$array[4] = array("e","f","g");
$array[5] = array("7","6","5");

$randnumber = rand(1,5); // ATTENTION : you had an "$" too much ...

if(array_search($string,$array[$randnumber])) // if $randnumber is 2, use $array2 and etc...
{
...
}
0
 

Author Comment

by:drakkarnoir
Comment Utility
Yes, but I'm very interested in variable variables, so if somebody could do it that way :)
0
 
LVL 1

Expert Comment

by:robinet02
Comment Utility
or, if you prefer to use your variable variables :

$array1 = array("a","b","c");
$array2 = array("1","2","3");
$array3 = array("a","b","c");
$array4 = array("e","f","g");
$array5 = array("7","6","5");

$randnumber = "array".rand(1,5);

if(array_search($string,$$randnumber)) // if $randnumber is 2, use $array2 and etc...
{
...
}
0
 
LVL 1

Accepted Solution

by:
robinet02 earned 500 total points
Comment Utility
in this way, the variable name is "array1", "array2", ... so you have to use $$randnumber in your array_search which gives ${'array1'} ... = $array1.

other solution, use ${'array'.$randnumber} in your array_search if you do not want to include the 'array' in your $randnumber variable :

$array1 = array("a","b","c");
$array2 = array("1","2","3");
$array3 = array("a","b","c");
$array4 = array("e","f","g");
$array5 = array("7","6","5");

$randnumber = rand(1,5);

if(array_search($string,${'array'.$randnumber})) // if $randnumber is 2, use $array2 and etc...
{
...
}
0
 

Author Comment

by:drakkarnoir
Comment Utility
Thanks for bearing with me there, and thanks for your help :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now