1

Task description is to use functions: count(), rand(), and strtoupper() to get random name printed out of my PHP array in all caps. Code: 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
?>

I got so far to choosing a random value from the array, but it gives me its number(position), not a name :/
Could you please point me in the right direction to solving that?

7 Answers 7

3

If array_rand returns an index, you should use that index into the $friends array to get its value.

$winner = array_rand($friends, 1);
$winner_name = strtoupper($friends[$winner]);
echo "<p>$winner_name</p>";
Sign up to request clarification or add additional context in comments.

Comments

2

If it returns the index, just fetch the value at that given index in the array:

$index = array_rand($friends, 1); // or $index = rand() % count($friends);
$winner = $friends[$index];

Comments

2

A simple way to getting Randdom value-form Array.

$color_array =["red","green","blue","light_orange"];
$color_array[rand(0,3)]

now every time you will get different colors from Array.

In your case.

 $friends[rand(0,8)]

Comments

0

You are getting an array index. Just do $friends[$winner] instead.

<?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
?>

Comments

0

you need to use like below

echo $friends[$winner];

Comments

0

Use the value from array_rand as a key for the $friends array.

$winner = strtoupper($friends[array_rand($friends, 1)]);

1 Comment

This one did not work for me when I replaced my definition of $winner.
-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>"; ?>

1 Comment

There is already an (accepted) answer that looks like yours. What makes your answer better than the other(s)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.