This sample code is giving me a headache. I'm trying to get 2 random values from the array. Delete the 2 entries that was chosen, and then continue until all elements is chosen. My problem is that the first 1-2 picks works fine, but then is chosen values that's not in the array, some times the same value is chosen 2 times...when removing the 2 unset-lines the code is ok... I belive it's about the reindexing of the array after deleting, but the line "$number=array_values($number)" should do the job...
<?php
$number=array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);//range(1,10);
//var_dump($number)." - ";//
for ($p = 1; $p <= 5; $p++) {
echo "Nr. :".$p."</br>";
echo implode(",",$number)."</br></br>";
$udtrak = array_rand($number,2); // udvælger 2
$pos[0] = array_search($udtrak[0], $number);
$pos[1] = array_search($udtrak[1], $number);
echo "Number: ".$number[$pos[0]]." Found at: " . $pos[0]."</br>";
echo "Number: ".$number[$pos[1]]." Found at: " . $pos[1]."</br>";
//unset($number[$pos[0]]);
//unset($number[$pos[1]]);
//var_dump($pos);
$number=array_values($number);
echo implode(",",$number)."</br></br>";
} // for $p
?>
Aucun commentaire:
Enregistrer un commentaire