1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Small PHP Problem!

Discussion in 'Black Hat SEO' started by facebookdude, Jun 25, 2011.

  1. facebookdude

    facebookdude Elite Member

    Joined:
    Feb 28, 2010
    Messages:
    1,506
    Likes Received:
    2,490
    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!
     
  2. confined

    confined Regular Member

    Joined:
    Jan 4, 2009
    Messages:
    216
    Likes Received:
    91
    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

    ?>
     
    • Thanks Thanks x 1
    Last edited: Jun 25, 2011
  3. masterfl

    masterfl Junior Member

    Joined:
    Apr 28, 2010
    Messages:
    160
    Likes Received:
    29
    Occupation:
    Fulltime Web Developer, Newbie IM
    Location:
    Philippines
    Home Page:
    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';
    ?>
    
     
    • Thanks Thanks x 1
  4. wkrappen91

    wkrappen91 Power Member

    Joined:
    Sep 9, 2010
    Messages:
    588
    Likes Received:
    720
    Location:
    127.0.0.1
    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?>
     
    • Thanks Thanks x 1
  5. facebookdude

    facebookdude Elite Member

    Joined:
    Feb 28, 2010
    Messages:
    1,506
    Likes Received:
    2,490
    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
     
  6. Rob Whisonant

    Rob Whisonant Junior Member

    Joined:
    Feb 25, 2011
    Messages:
    112
    Likes Received:
    38
    Use a two dimensional array holding the animal, url pairs.
     
    • Thanks Thanks x 1
  7. ostermeier

    ostermeier Newbie

    Joined:
    Apr 14, 2010
    Messages:
    19
    Likes Received:
    9
    Use the switch statement that confined recommended.
     
    • Thanks Thanks x 1
    Last edited: Jun 25, 2011
  8. masterfl

    masterfl Junior Member

    Joined:
    Apr 28, 2010
    Messages:
    160
    Likes Received:
    29
    Occupation:
    Fulltime Web Developer, Newbie IM
    Location:
    Philippines
    Home Page:
    either of the 2 posts above mine will do the trick
     
    • Thanks Thanks x 1
  9. risefromdeath

    risefromdeath Power Member

    Joined:
    Jul 1, 2009
    Messages:
    650
    Likes Received:
    107
    use switch as explained by confined :)
     
    • Thanks Thanks x 1
  10. confined

    confined Regular Member

    Joined:
    Jan 4, 2009
    Messages:
    216
    Likes Received:
    91
    *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.
     
    • Thanks Thanks x 1
  11. wkrappen91

    wkrappen91 Power Member

    Joined:
    Sep 9, 2010
    Messages:
    588
    Likes Received:
    720
    Location:
    127.0.0.1
    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...
     
    • Thanks Thanks x 1
  12. facebookdude

    facebookdude Elite Member

    Joined:
    Feb 28, 2010
    Messages:
    1,506
    Likes Received:
    2,490
    Can't thank you guys enough! Works perfectly!
     
  13. DeuceX

    DeuceX Newbie

    Joined:
    May 24, 2010
    Messages:
    22
    Likes Received:
    0
    Sorry i dont know lol.
    sorry cant help