从 PHP 数组中获取随机短语? [英] Get random phrases from an array in PHP?
本文介绍了从 PHP 数组中获取随机短语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的短语,例如:test 1、test 2、test 3,现在如何在随机模式下显示在加载页面上?
I have this phrases ex: test 1, test 2, test 3, now how to show on load page in random mode?
ex 函数
function random()
{
$array = ['test 1', 'test 2', 'test 3'];
return $random_array;
}
推荐答案
将它们放在一个数组中并使用 array_rand 获取随机密钥.
Put them in an array and use array_rand to get a random key.
function random()
{
$phrases = array(
'random test 1',
'random test 2',
'random test 3'
);
return $phrases[array_rand($phrases)];
}
这篇关于从 PHP 数组中获取随机短语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文