Small PHP Problem!

Proxy Cake

Elite Member
Jr. VIP
Joined
Feb 28, 2010
Messages
5,501
Reaction score
4,420
Hey guys I just need a little bit of help! Basically I have a random array like this for example:

Code:
<?php
$animalarray = array("cat", "dog", "beast", "lizard", "horse", "lion");
$random_animal = $animalarray[array_rand($animalarray)];
?>
<?php
echo "I recognize the $random_animal" ?>

What I want to do from this is create another variable based on what gets picked. So I'm thinking an if statement.

Any help is appreciated!
 
What exactly do you want to achieve?

PHP:
<?php
$animalarray = array("cat", "dog", "beast", "lizard", "horse", "lion");
$random_animal = $animalarray[array_rand($animalarray)];

switch($random_animal){

case 'cat': 
$url= 'http://google.com';
break;

case 'dog':
$url= 'http://yahoo.com';
break;

//copy above 3 lines for each variable and change values.

default:
break;


} //end switch

?>
 
Last edited:
Code:
<?php
$animalarray = array("cat", "dog", "beast", "lizard", "horse", "lion");

//this will create $cat OR $dog... with value 'lorem ipsum'
${$animalarray[array_rand($animalarray)]) = 'lorem ipsum';
?>
 
PHP:
<?php
$animalarray = array( 0=>"lion", 1=>"cat", 2=>"dog", 3=>"beast", 4=>"lizard", 5=>"horse");
$random=rand(0,count($animalarray)-1);
$random_animal = $animalarray[$random];
?>
<?php
echo "I recognize the $random_animal" ?>
 
Let me be more clear, when the random animal is chosen a variable called 'URL' needs to change. So if 'cat' is chosen the the variable 'URL' will be 'http://google.com/'. If 'beast' is chosen 'URL' will be 'http://yahoo.com/' etc.

Thanks so much for your help so far guys, got some good coders here :D
 
*I updated my post to reflect on what facebookdude wants, with some instructions, hopefully it helps you. basically just copy the three lines including the break and it should work.
 
PHP:
<?php
$anymalarray[0]['animal']="tiger";
$anymalarray[0]['url']="http://google.com";
$anymalarray[1]['animal']="lion";
$anymalarray[1]['url']="http://internet.com";
$anymalarray[2]['animal']="poodle";
$anymalarray[2]['url']="http://chip.com";
$anymalarray[3]['animal']="cat";
$anymalarray[3]['url']="http://dog.com";
$random=rand(0,count($animalarray)-1);
$random_animal = $animalarray[$random]['animal'];
$random_url = $animalarray[$random]['url'];
?>
<?php
echo "$random_animal was picked, and the url is $random_url" ?>

Not tested, should work...
 
Can't thank you guys enough! Works perfectly!
 
Back
Top