note 106446 rejected from function.array-rand by dave
| From: | dave@php.net | Date: | Thu, 10 Nov 2011 07:52:55 +0000 |
| Subject: | note 106446 rejected from function.array-rand by dave | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184364@lists.php.net to get a copy of this message | ||
Note Submitter: stecnolo at gmail dot com
----
Hi, this code is perfect to me...
<?php
$n = 15;
$data = range(1, 20);
$rand = array_rand($data,$n);
for($i=0; $i<$n; $i++)
{
echo $rand[$i]."<br>";
}
?>
but I don't know how to fill the number less than 4 size chars.
example, if the value is 21, so this displays as 0021
I have used this code, but it don`t works fine
<?php
$n = 15;
$data = range(0, 9999);
$num = $data;
$rand = array_rand($num,$n);
for($i=0; $i<$n; $i++)
{
if ($rand <= 9){
$num = '000'.$rand;
}else if($rand <= 99){
$num = '00'.$rand;
}else if ($rand <= 999){
$num = '0'.$rand;
}else{
$num = $rand;
}
echo $num[$i]."<br>";
}
?>
Thanks for your help