如何在array_slice($ some_content,0,2,true)中打印随机数组?

时间:2019-01-09 09:38:53

标签: php arrays

假设:

$some_content = array("abc", "def", "ghi", "jkl", "mno", "pqr", "stu");

现在我使用:

$slice = array_slice($some_content, 0, 2, true);

仅选择前两个值“ abc”和“ def”。

我的问题是我想要一个显示“ abc”(固定)的代码,但随机选择数组的任何其他值,例如“ ghi”或“ jkl”或“ pqr”等。该怎么做?

2 个答案:

答案 0 :(得分:0)

类似这样的东西:

$some_content = array("abc", "def", "ghi", "jkl", "mno", "pqr", "stu");
$fixedEl = array_shift($some_content);
$randomEl = array_rand($some_content);

答案 1 :(得分:0)

使用此技巧:

$source = array("abc", "def", "ghi", "jkl", "mno", "pqr", "stu");
$pick = [$source[0], $source[array_rand(array_slice($source, 1, null, true))]];
print_r($pick);