快速问题:
我怎么能做一个像3个数组填充不同单词的PHP脚本,脚本从中挑出一个并打印消息?
我想出了这个,但它不起作用:
<?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}";
?>
同样使用我的脚本,即使它有效,我还要添加一个新行,我添加的每一个...是否有更好的解决方案?
答案 0 :(得分:0)
这不是一个非常干净的脚本,但这是你的工作。
##
## 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 ]}";
稍微清洁的版本
$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) ]}";