Solved

Variable Variables

Posted on 2004-04-24
5
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:robinet02
ID: 10910774
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
ID: 10910779
Yes, but I'm very interested in variable variables, so if somebody could do it that way :)
0
 
LVL 1

Expert Comment

by:robinet02
ID: 10910788
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
ID: 10910793
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
ID: 10910804
Thanks for bearing with me there, and thanks for your help :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 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