Jump to content

Remove element from an array


PKENGLISH

Recommended Posts

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
Share on other sites

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
Share on other sites

$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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.