user3220962 user3220962 - 1 month ago 11
PHP Question

generate 3 words out of 3 arrays randomly

quick Question:

How can i do a little php script that has like 3 arrays filled with different words and the script picks one out of each and prints the message?

i came up with this but it doesnt work:

<?php header('Content-type: text/plain; charset=utf-8');

##
## evolve script test
##


$a1 = "blue";
$a2 = "red";
$a3 = "green";
$a4 = "pink";
$a5 = "black";

$b1 = " cute";
$b2 = " ugly";
$b3 = " big";
$b4 = " small";
$b5 = " loud";

$c1 = " dog";
$c2 = " cat";
$c3 = " beever";
$c4 = " bee";
$c5 = " ape";


$rm1 = array($a1, $a2, $a3, $a4, $a5);
$rm2 = array($b1, $b2, $b3, $b4, $b5);
$rm3 = array($c1, $c2, $c3, $c4, $c5);

$randomizer1 = array_rand($rm1, 1);
$randomizer2 = array_rand($rm2, 1);
$randomizer2 = array_rand($rm3, 1);

// echo $rm[$random_keys];
echo "evolves into a , {$randomizer1} {$randomizer2} {$randomizer3}";
?>


Also with my script even if it works i have toadd a new line for every thign i add.. is there a better solution?

Answer

Not a very clean script but this is your working.

##
## evolve script test
##


$a1 = "blue";
$a2 = "red";
$a3 = "green";
$a4 = "pink";
$a5 = "black";

$b1 = " cute";
$b2 = " ugly";
$b3 = " big";
$b4 = " small";
$b5 = " loud";

$c1 = " dog";
$c2 = " cat";
$c3 = " beever";
$c4 = " bee";
$c5 = " ape";


$rm1 = array($a1, $a2, $a3, $a4, $a5);
$rm2 = array($b1, $b2, $b3, $b4, $b5);
$rm3 = array($c1, $c2, $c3, $c4, $c5);

$randomizer1 = array_rand($rm1, 1);
$randomizer2 = array_rand($rm2, 1);
$randomizer3 = array_rand($rm3, 1);

// echo $rm[$random_keys];
echo "evolves into a , {$rm1[ $randomizer1 ]} {$rm2[ $randomizer2 ]} {$rm3[ $randomizer3 ]}";

Slightly cleaner version

$a = array( "blue", "red", "green", "pink","black" );
$b = array( " cute", " ugly", " big", " small", " loud" );
$c = array( " dog", " cat", " beever", " bee", " ape" );

echo "evolves into a , {$a[ rand( 0, 4) ]} {$b[ rand( 0, 4) ]} {$c[ rand( 0, 4) ]}";