Leçon N° 05 : Tableaux
La fonction array_rand¤
Prend une ou plusieurs valeurs, au hasard dans un tableau
Notes de page
Source: lesson_05_p_array_rand.phpRésultat
<?php
/*La fonction array_rand() renvoie aléatoirement 
un nombre fixe de valeurs du tableau*/
$fruits = array("pomme""poire""pêche",
     
"abricot""fraise""cassis","melon");
$troisFruits array_rand($fruits3);
echo 
"<table width=80% border=1 align=center><tr>
    <th>Tableau_1</th><th>Trois fruits au hasard</th><tr><td>"

//tableau $cars_rented
echo "<table border=1 align=center>
    <tr><th>Valeur</th><th>Clé</th></tr>"
;
foreach(
$fruits as $maClé=>$maValeur)
{
    echo 
"<tr><td>$maValeur</td><td>$maClé</td></tr>";
}
echo 
"</table></td><td align=center>";
//tableau $cars_rented_count
echo "<table border=1 align=center>
    <tr><th>Valeur</th><th>Clé</th><th>Fruit</tr>"
;
foreach(
$troisFruits as $maClé=>$maValeur)
{
    echo 
"<tr><td>$maValeur</td><td>$maClé</td>
        <td>
$fruits[$maValeur]</td></tr>";
}
echo 
"</table></td><td align=center>";
echo 
"</td></tr></table>";
?>

Tableau_1Trois fruits au hasard
ValeurClé
pomme0
poire1
pêche2
abricot3
fraise4
cassis5
melon6
ValeurCléFruit
00 pomme
11 poire
32 abricot

Tous droits réservés. 2005-2020