On Thu, 2002-04-25 at 14:01, Kevin Stone wrote: > About all I would suggest is that you eliminate the unecessary function > calls substr and explode to optimize your code. I doubt prebuilt PHP > functions will help you here. This is perfectly valid method for deleting > elements in an array and it's about as fast as you're ever going to get. :)
But PHP builtins will do exactly this, around 3 times faster, too. :) <?php error_reporting(E_ALL); $listitems = array ('grill', 'world', 'bar', 'foo', 'world', 'foo'); // To delete one value: $item_to_delete = 'foo'; // To delete multiple values at once: // $item_to_delete = array('foo', 'world'); $listitems = array_diff($listitems, (array)$item_to_delete); print_r($listitems); ?> If you need the array reindexed, just array_values() it when you're done. > <? > $listitems = array ('grill', 'bar', 'foo', 'world'); > $item_to_delete = 'foo'; > > // Loop through listitems array. > for ($i=0; $i<count($listitems); $i++) > { > // Ignore item to delete. > if ($listitems[$i] != $item_to_delete) > { > // Save all other items in tmplist array. > $tmplist[] = $listitems[$i]; > } > } > $listitems = $tmplist; //new array contains (grill, bar, world) > ?> > > -Kevin > > ----- Original Message ----- > From: "Liam Gibbs" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Thursday, April 25, 2002 2:27 PM > Subject: Re: [PHP] Array function to delete > > > > Thanks for all your help, everyone, but both > > suggestions (unset and array_slice) pretty much didn't > > improve on my current way. I was jsut trying to find a > > faster way, but unset doesn't seem to be working > > properly (but I'll need to fiddle more) and the > > array_slice way is just too intensive. > > > > for($counter = 0; $counter < count($listitems); > > $counter++) { > > if($listitems[$counter] != $item) > > $newlist .= ",$listitems[$counter]"; > > } > > $newlist = substr($newlist, 1, strlen($newlist)); > > $listitems = explode("," $newlist); > > > > __________________________________________________ > > Do You Yahoo!? > > Yahoo! Games - play chess, backgammon, pool and more > > http://games.yahoo.com/ > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- Torben Wilson <[EMAIL PROTECTED]> http://www.thebuttlesschaps.com http://www.hybrid17.com http://www.inflatableeye.com +1.604.709.0506 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php