Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:68060 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 90083 invoked from network); 3 Jul 2013 21:01:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Jul 2013 21:01:43 -0000 Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.169 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.214.169 mail-ob0-f169.google.com Received: from [209.85.214.169] ([209.85.214.169:57305] helo=mail-ob0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id EA/71-15343-63194D15 for ; Wed, 03 Jul 2013 17:01:42 -0400 Received: by mail-ob0-f169.google.com with SMTP id up14so735380obb.0 for ; Wed, 03 Jul 2013 14:01:40 -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=mwI7RhMAimncWo4VOx07sFo30NS1l7JIhgvrkjdwtp0=; b=lkuWkCz/86qRNDCh1Kar8yiuWvCY0A42PDnxB7XRDrSaIOSpNigvQZQ9ObeH+TsvPP xaJb5U3vU5vCZ5a3xeUaWxBZrX2c6kyn8Ej1a3e6/GHvzDlYRuFXyi0sUbx0DFAMxmJV N9urWLgn5gh2fbfZ9O7BcVQae+KM9+nz4wanD38fl88lSu1L+7mvIC/xVim29RkbcL8D LXWdr4hGlioT3/Pl7TRg46hmdsVGqoVsQBgIE/fjmJLbtStjUo5xsLChv+xsN2KZtjZu pPngL69DqtHRV712q/dEnLKfzsduxQ1lcfWvaIWap0/I8jMpp/ykDzRPmQO0vcVUc34g lgaQ== MIME-Version: 1.0 X-Received: by 10.60.117.233 with SMTP id kh9mr2786260oeb.58.1372885299810; Wed, 03 Jul 2013 14:01:39 -0700 (PDT) Received: by 10.182.89.170 with HTTP; Wed, 3 Jul 2013 14:01:39 -0700 (PDT) In-Reply-To: References: Date: Wed, 3 Jul 2013 23:01:39 +0200 Message-ID: To: Chris London Cc: PHP internals Content-Type: multipart/alternative; boundary=047d7b3a92a09cd6f204e0a1c42d Subject: Re: [PHP-DEV] New feature: additional syntax proposal for Foreach From: nikita.ppv@gmail.com (Nikita Popov) --047d7b3a92a09cd6f204e0a1c42d Content-Type: text/plain; charset=ISO-8859-1 On Wed, Jul 3, 2013 at 10:01 PM, Chris London wrote: > Hello community, > > This is my first post here. I hope to improve my participation in the > community and give back to the technology that has done so much for my > career. My first attempt is to propose an additional syntax for the > Foreach statement. > > Background: > > There are times in our code where we can leave things blank that we don't > need, such as: > > for ( ; ; ) { > > } > > - and - > > list( , $foo, $bar) = array('ignore', 'myFoo', 'myBar'); > > Proposal: > > I have found over the years that there are times when I want to loop > through an associative array but I don't need the value. I would like to > allow the following syntax: > > foreach ($array as $key => ) { > > } > > Please let me know your thoughts. > > Thanks > Chris London > While it does sometimes happen that you do not need the value, it's not particularly common and as such I don't think it's necessary to add additional syntax for this case. It's fairly straightforward to abstract this behavior away into a separate function used as follows: foreach (keys($iterable) as $key) { ... } Where the keys function is defined as: function keys($iterable) { foreach ($iterable as $key => $_) { yield $key; } } This is something you write once (with the slightly ugly $key => $_), but can then always use to have clear code :) Of course, as already said in the other mail, for arrays the keys() function already exists as array_keys(), so you don't even have to write anything yourself. Nikita --047d7b3a92a09cd6f204e0a1c42d--