PKENGLISH Posted February 7, 2008 Share Posted February 7, 2008 Hi, This is pretty simple i think but, I have an array that I would like to randomly call a value from and then delete that item from the array. So for example: call value #3 on an array thats 12 values long, after its been called the array is only 11 values long. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
teng84 Posted February 7, 2008 Share Posted February 7, 2008 <a href="http://www.php.net/manual/en/function.array-diff.php">array_diff</a> <a href="http://www.php.net/manual/en/function.array-pop.php"> array_pop</a> Quote Link to comment Share on other sites More sharing options...
trq Posted February 7, 2008 Share Posted February 7, 2008 <?php unset($arr[3]); ?> Quote Link to comment Share on other sites More sharing options...
PKENGLISH Posted February 7, 2008 Author Share Posted February 7, 2008 Thanks for the fast response, is there any way to have all the items above the array shift down when the item is deleted? Here is what I am actually doing: <?php for($i=0; $i<12; $i++){ $random_key = rand(0, sizeof($partners)-1); echo $partners[$random_key]."<br /> "; unset($partners[$random_key]); } ?> with unset it still randomly pulls up the empty item. Quote Link to comment Share on other sites More sharing options...
teng84 Posted February 7, 2008 Share Posted February 7, 2008 $array = array("orange", "banana", "apple", "raspberry"); $z = count($array); while(count($z)>0){ $x = shuffle($array); $ary = array($array[$x]); if(count($array) == 1){ array_pop($array); break; } $array = array_diff($array,$ary); print_r($array); $z = count($array); echo '<br>'; } this should work Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.