任务描述是使用函数:count(),rand()和strtoupper()来获取所有大写的PHP数组中的随机名称。 代码:http://ideone.com/nwjyoa
<?php
// Create an array and push on the names
$friends=array("Mike", "Ondrej", "Honza", "Danca", "Misa", "Verca");
array_push($friends, "Michal", "Vendulka", "Daniela");
// Sort the list
sort($friends);
// Randomly select a winner!
$winner = array_rand($friends, 1);
echo "<p>$winner</p>";
// Print the winner's name in ALL CAPS
?>
我到目前为止从阵列中选择一个随机值,但它给出了它的编号(位置),而不是名称:/
你能指点我解决问题吗?
答案 0 :(得分:2)
如果它返回索引,只需获取数组中该给定索引处的值:
$index = array_rand($friends, 1); // or $index = rand() % count($friends);
$winner = $friends[$index];
答案 1 :(得分:1)
如果array_rand
返回索引,则应将该索引用于$friends
数组以获取其值。
$winner = array_rand($friends, 1);
$winner_name = strtoupper($friends[$winner]);
echo "<p>$winner_name</p>";
答案 2 :(得分:0)
您正在获取数组索引。只做$ friends [$ winner]而不是。
<?php
// Create an array and push on the names
$friends=array("Mike", "Ondrej", "Honza", "Danca", "Misa", "Verca");
array_push($friends, "Michal", "Vendulka", "Daniela");
// Sort the list
sort($friends);
// Randomly select a winner!
$winner = array_rand($friends, 1);
echo "<p>$friends[$winner]</p>";
// Print the winner's name in ALL CAPS
?>
答案 3 :(得分:0)
你需要使用如下
echo $friends[$winner];
答案 4 :(得分:0)
使用array_rand
中的值作为$friends
数组的键。
$winner = strtoupper($friends[array_rand($friends, 1)]);
答案 5 :(得分:0)
获取Randdom值形式数组的简单方法。
let calledStatus = 0; // some flag variable to remember if function is called
window.onscroll = function(){
element = document.querySelector( '.some element' );
clientRect = element.getBoundingClientRect();
if( clientRect.top < window.innerHeight && clientRect.top > ( clientRect.height * -1) && calledStatus == 0){
//call your function or do other stuff
console.log('called' )
calledStatus = 1;
}
}
现在每次您从Array中获得不同的颜色。
以您为例。
$color_array =["red","green","blue","light_orange"];
$color_array[rand(0,3)]
答案 6 :(得分:-3)
<?php
// Create an array and push on the names
// of your closest family and friends
$family_friends=array();
array_push($family_friends, "Marlon");
array_push($family_friends, "Johanna");
array_push($family_friends, "Leonary");
array_push($family_friends, "Sueann");
array_push($family_friends, "Olucho");
array_push($family_friends, "Allyah");
array_push($family_friends, "Reyna");
array_push($family_friends, "Cinil");
array_push($family_friends, "Carl");
array_push($family_friends, "Martain");
array_push($family_friends, "Christell");
array_push($family_friends, "Joy");
// Sort the list
$sorted = sort($family_friends);
$counted = count($family_friends);
print $counted;
// Randomly select a winner!
$win = array_rand($family_friends,1);
// Print the winner's name in ALL CAPS
$winner = strtoupper($win);
print "<p>$family_friends[$winner] </p>"; ?>