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. Link to comment https://forums.phpfreaks.com/topic/89828-remove-element-from-an-array/ 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> Link to comment https://forums.phpfreaks.com/topic/89828-remove-element-from-an-array/#findComment-460359 Share on other sites More sharing options...
trq Posted February 7, 2008 Share Posted February 7, 2008 <?php unset($arr[3]); ?> Link to comment https://forums.phpfreaks.com/topic/89828-remove-element-from-an-array/#findComment-460360 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. Link to comment https://forums.phpfreaks.com/topic/89828-remove-element-from-an-array/#findComment-460367 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 Link to comment https://forums.phpfreaks.com/topic/89828-remove-element-from-an-array/#findComment-460374 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.