Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62204 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 1273 invoked from network); 15 Aug 2012 20:27:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Aug 2012 20:27:26 -0000 Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.42 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.215.42 mail-lpp01m010-f42.google.com Received: from [209.85.215.42] ([209.85.215.42:39071] helo=mail-lpp01m010-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 28/25-08779-D260C205 for ; Wed, 15 Aug 2012 16:27:26 -0400 Received: by lahl5 with SMTP id l5so1110471lah.29 for ; Wed, 15 Aug 2012 13:27:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=4OgzDcp4jp8Z5LAR2QWaAKPltJjX7rDM3g7YnKAaRbA=; b=KNg+WNoMw61a84G+JzLKBESrSCRtihWQicPUyhHdJK5gbu2+wiYqsykooFZIPEzxdR UuG3GeDgUT6Ku4OiEQq/a8SiCm4Hr3BMKJqnPq49sqImVo/P+OnrzJvBUWMREqKCOJRC Aepmw8AwPR4EtewllOnPEK1OBK6Et3fb0uYA9ciDGkWDrAbzrnmXhEMn67b49nNxHzML H9Ln4let3ZNRNkho4jpsWsXEBBdbAdrmDSHaKdbgKS8RSE1lb869tAx4Eb1ICTlAMt8c DWdvsNBYW0Inyn3Q8tAQT+SeJGfySAu3Y0swBp+8yBhtmkcWBlP/gyrxaE1QQbpACoue vCfA== MIME-Version: 1.0 Received: by 10.152.144.168 with SMTP id sn8mr4842484lab.1.1345062442774; Wed, 15 Aug 2012 13:27:22 -0700 (PDT) Received: by 10.152.122.51 with HTTP; Wed, 15 Aug 2012 13:27:22 -0700 (PDT) In-Reply-To: <502C04FB.2010000@sugarcrm.com> References: <502C04FB.2010000@sugarcrm.com> Date: Wed, 15 Aug 2012 22:27:22 +0200 Message-ID: To: Stas Malyshev Cc: Rasmus Schultz , "internals@lists.php.net" Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] removing an item from an array From: nikita.ppv@gmail.com (Nikita Popov) On Wed, Aug 15, 2012 at 10:22 PM, Stas Malyshev wrote: > Hi! > >> How come there is no straight-foward obvious way to simply remove a given >> value from an array? > > The same reason there's no simple way to undefine variable whose value > is 42 without knowing the variable name. Array is a container indexed by > keys, not values. So if you've got just a value, there's no way to know > if it's in the container at all, and if it is, where it is, except for > going through all the values and checking if any of them is equal to > what you nedd. > >> 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. In addition to that, one should be aware that a value can exist multiple times in an array, whereas keys are unique. So there are infinitely many possible deletion strategies. Btw, deleting all values (not just the first) is also very easy currently: foreach (array_keys($array, $delValue) as $key) { unset($array[$key]); }