Re: Array function to delete
| From: | Kevin Stone | Date: | Thu, 25 Apr 2002 21:01:47 +0000 |
| Subject: | Re: Array function to delete | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-94666@lists.php.net to get a copy of this message | ||
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. :)
<?
$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" <liamgibbs@yahoo.com>
To: <php-general@lists.php.net>
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
>
>