Haal willekeurig item uit array

Elk item van deze array is een nummer.

Hoe krijg ik een willekeurig item van $items?


Antwoord 1, autoriteit 100%

echo $items[array_rand($items)];

array_rand()


Antwoord 2, autoriteit 7%

Als je het niet erg vindt om hetzelfde item op een ander moment opnieuw te kiezen:

$items[rand(0, count($items) - 1)];


Antwoord 3, autoriteit 3%

Gebruik PHP Rand-functie

<?php
  $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
  $rand_keys = array_rand($input, 2);
  echo $input[$rand_keys[0]] . "\n";
  echo $input[$rand_keys[1]] . "\n";
?>

Meer hulp


Antwoord 4, autoriteit 2%

gebruik array_rand()

zie php-handleiding -> http://php.net/manual/en/function.array-rand.php

Other episodes