Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62211 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 33257 invoked from network); 16 Aug 2012 05:40:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Aug 2012 05:40:29 -0000 Authentication-Results: pb1.pair.com smtp.mail=lars.schultz@toolpark.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=lars.schultz@toolpark.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain toolpark.com from 195.49.42.12 cause and error) X-PHP-List-Original-Sender: lars.schultz@toolpark.com X-Host-Fingerprint: 195.49.42.12 mail1.screenlight.ch Received: from [195.49.42.12] ([195.49.42.12:59070] helo=mail1.screenlight.ch) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 60/D9-08779-AC78C205 for ; Thu, 16 Aug 2012 01:40:27 -0400 Received: from [192.168.1.112] ([192.168.1.112]) (authenticated user lars.schultz@toolpark.com) by mail1.screenlight.ch (Kerio Connect 7.0.2 patch 1) (using TLSv1/SSLv3 with cipher AES256-SHA (256 bits)) for internals@lists.php.net; Thu, 16 Aug 2012 07:40:21 +0200 Message-ID: <502C87C4.1010600@toolpark.com> Date: Thu, 16 Aug 2012 07:40:20 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 MIME-Version: 1.0 To: internals@lists.php.net References: <502C04FB.2010000@sugarcrm.com> In-Reply-To: <502C04FB.2010000@sugarcrm.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] removing an item from an array From: lars.schultz@toolpark.com (Lars Schultz) Am 15.08.2012 22:22, schrieb Stas Malyshev: >> Just look at the number of horrible ways people solve this obvious problem: > > I see: > if(($key = array_search($del_val, $messages)) !== false) { > unset($messages[$key]); > } > > Nothing horrible here. > One thing that should be noted in this case and any solution that relies on unset() is that even though its simple and fast, it will not result in a properly indexed array. The same goes for any array_diff based solution. I tried and compared the following solutions and ordered them according to their performance. The fastest (and with a correct result) solution is based on array_slice. Why this is the case I can not say...I am not arguing for another array-function (as there are so many already)...but I certainly have my own array_remove implementation, since it's such a common use-case. function array_remove_slice(&$haystack,$needle){ while ( true ) { $pos = array_search($needle,$haystack,true); if ( $pos === false ) return; $haystack = array_merge( array_slice($haystack,0,$pos) , array_slice($haystack,$pos+1) ); } } /* ~1.5 times slower than slice */ function array_remove_unset(&$haystack,$needle){ while ( true ) { $pos = array_search($needle,$haystack,true); if ( $pos === false ) break; unset($haystack[$pos]); } } /* ~2.3 times slower than slice */ function array_remove_loop(&$haystack,$needle){ $result = array(); foreach( $haystack as $value ) { if ( $needle == $value ) continue; $result[] = $value; } $haystack = $result; } /* ~3.5 times slower than slice */ function array_remove_diff(&$haystack,$needle){ $haystack = array_diff($haystack,array($needle)); }