开发者

Display a number from array in PHP

开发者 https://www.devze.com 2023-02-05 04:20 出处:网络
Hello I would like to have a code that displays randomly from an array a number and displays i开发者_如何学Ct.

Hello I would like to have a code that displays randomly from an array a number and displays i开发者_如何学Ct. For example this code

<?php
$firstquarter = array(1 => 'January', 'February', 'March');
print_r($firstquarter);
?>

will return

Array
(
    [1] => January
    [2] => February
    [3] => March
)

Instead of this, I would like to display on


A simple function called array_rand

example:

$selection = array_rand($my_array);

Documentation


Use rand()

$iIndex = rand(1, count($firstquarter));
echo $firstquarter[$iIndex];

This assumes you have an one indexed array with consecutive keys.


You need array_rand():

echo $firstquarter[array_rand($firstquarter)];

I'm not sure if this is what you want but here it goes:

echo mt_rand(intval(date('H') / 3) * 4, intval(date('H') / 3) * 5);
0

精彩评论

暂无评论...
验证码 换一张
取 消