Jump to content
php.lv forumi

random, array_rand vai shuffle


Recommended Posts

Posted

<?php

$input = array('xxx', 'yyy', 'zzz');

$rand_keys = array_rand($input, 1);

echo $input[$rand_keys['0']] . "\n";

?>

 

vai

 

<?php

$input = array('xxx', 'yyy', 'zzz');

shuffle($input);

echo $input[0] . "\n";

?>

 

ir kāds vēlamāks par otru variants?

Posted

Nevajag padot stringu tur, kur vajadzīgs integers.

Un nevajag lieki konkatenēt stringus.

<?php
$input = array('xxx', 'yyy', 'zzz');
$rand_keys = array_rand($input, 1);
echo $input[$rand_keys[0]], "\n";
?>

×
×
  • Create New...