Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67953 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 65546 invoked from network); 27 Jun 2013 14:54:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Jun 2013 14:54:39 -0000 Authentication-Results: pb1.pair.com smtp.mail=g.b.yahav@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=g.b.yahav@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.47 as permitted sender) X-PHP-List-Original-Sender: g.b.yahav@gmail.com X-Host-Fingerprint: 209.85.220.47 mail-pa0-f47.google.com Received: from [209.85.220.47] ([209.85.220.47:40387] helo=mail-pa0-f47.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8B/66-34034-5225CC15 for ; Thu, 27 Jun 2013 10:54:34 -0400 Received: by mail-pa0-f47.google.com with SMTP id kl14so1146444pab.20 for ; Thu, 27 Jun 2013 07:54:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=p4WEVE6IUksR1VHlezjnPGJcQ/70woQt3bcZuCD1lRM=; b=mpeh4nR/QLqtpREmB9v/vsU56EzJzEGdG66+JH9ZK0vWlC0I7lg+MC+SikE/LpIVfv FwRs9b5luviAYHK5xno4fJFRTQP5x+xgsUbS12J/IohPLKVhf9LL7tCWKBzTIADS1h1r evn6+iwMSwADWNnrRnl8dF/pFCMupRi7/XgpMHSnzNLJN5SwXdnfn+uob4DLaFcjp8Nj 1oUoi8DZ5pmy5ha+DoAyvBrYwQil875vCdCIhCD1Eha8PIocKk2iGTA/qP5M3uYY2TdF Sjmy83L3EbHeXh7r8SEZacTdUsadWBYMaVRRm9jSUTIxEI3qZQnN7C/bjQMsXByx6Vl1 doKA== X-Received: by 10.66.144.170 with SMTP id sn10mr6487610pab.42.1372344867084; Thu, 27 Jun 2013 07:54:27 -0700 (PDT) MIME-Version: 1.0 Received: by 10.70.92.132 with HTTP; Thu, 27 Jun 2013 07:54:04 -0700 (PDT) In-Reply-To: <51CC4BE8.4010203@b1-systems.de> References: <4ED7146272E04A47B986ED49E771E347BB4DF6F344@Ikarus.ameusgmbh.intern> <51CC4BE8.4010203@b1-systems.de> Date: Thu, 27 Jun 2013 17:54:04 +0300 Message-ID: To: Ralf Lang Cc: PHP internals Content-Type: multipart/alternative; boundary=047d7b6d7e024fe16b04e023f02b Subject: Re: [PHP-DEV] New syntax for multidimensional array loop with foreach From: g.b.yahav@gmail.com (Yahav Gindi Bar) --047d7b6d7e024fe16b04e023f02b Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On Thu, Jun 27, 2013 at 5:27 PM, Ralf Lang wrote: > On 27.06.2013 16:10, Christian Stoller wrote: > > Hi internals, > > > > during my current work I had an idea for shorter array iteration with > foreach. I haven=92t seen such a syntax until now, but I think it is easy= to > understand ;-) > > > > Maybe you know the case where you have to iterate over all values of a > 2D (or more) array: > > > > $count =3D 0; > > foreach ($array as $key =3D> $innerArray) { > > foreach ($innerArray as $innerKey =3D> $value) { > > $count +=3D $value; > > // and do something with $key and $innerKey > > } > > } > > > > The new syntax could make it shorter and faster to write... but maybe > it's a bit too confusing? > > > > $count =3D 0; > > foreach ($array as $key =3D> $innerArray as $innerKey =3D> $value) { > > $count +=3D $value; > > // and do something with $key and $innerKey > > } > > What do you think? > > How would a break or continue on this behave? Would it end the inner or > the outer circle? > > I guess that the usage of continue/break with specific number is the best in this case... foreach ($array as $key =3D> $innerArray as $innerKey =3D> $value) { $count +=3D $value; if ($count % 2 =3D=3D 0) { // for instance... break 2; // End loop } else if ( $count % 3 =3D=3D 0 ) { break; // End inner loop } else if ( $count % 4 =3D=3D 0) { continue 2; // Continue outer loop } continue; // Continue inner loop } Personally, this syntax (the multi-dims loop) seems unreadable... > > -- > Ralf Lang > Linux Consultant / Developer > Tel.: +49-170-6381563 > Mail: lang@b1-systems.de > B1 Systems GmbH > Osterfeldstra=DFe 7 / 85088 Vohburg / http://www.b1-systems.de > GF: Ralph Dehner / Unternehmenssitz: Vohburg / AG: Ingolstadt,HRB 3537 > > --047d7b6d7e024fe16b04e023f02b--