Re: Removing All Instances From An Array

From: Date: Thu, 13 Jul 2000 03:33:09 +0000
Subject: Re: Removing All Instances From An Array
Groups: php.general 
Request: Send a blank email to php-general+get-6413@lists.php.net to get a copy of this message
Andrew <fisherres@msn.com> wrote: >Hello, > >1. I have an array $array = array (1,2,3,3,3,4,5,6,7,8,9,9); >2. The variable $picked has the value 2. >3. If I print the value of $array[$picked] I see the number three. >4. Well instead of printing the number three I want to remove all instances >of 3 from the array. So, my array should look like $array = array >(1,2,4,5,6,7,8,9,9); > > Basically, whatever the value of $array[picked] is, I want to remove all >instances of that value from the array. Well, if the array is always going to be sorted like that you could do something like... $i = $picked + 1; while( $array[$i] == 3 ) { $i++; } array_splice($array, $picked, $i - $picked); If the array isn't sorted, you could either sort it first, or just loop through and skipping the 3's, put the rest into a new array... -philip

« previous php.general (#6413) next »