Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67949 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 58166 invoked from network); 27 Jun 2013 14:27:48 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Jun 2013 14:27:48 -0000 Authentication-Results: pb1.pair.com smtp.mail=krebs.seb@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=krebs.seb@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.179 as permitted sender) X-PHP-List-Original-Sender: krebs.seb@gmail.com X-Host-Fingerprint: 74.125.82.179 mail-we0-f179.google.com Received: from [74.125.82.179] ([74.125.82.179:48565] helo=mail-we0-f179.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id BC/D4-34034-3EB4CC15 for ; Thu, 27 Jun 2013 10:27:48 -0400 Received: by mail-we0-f179.google.com with SMTP id w59so622505wes.38 for ; Thu, 27 Jun 2013 07:27:45 -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=V9fpxJbkOJf8qzLoRmQLD1tE1vMWTuqTgrJiWiA5zPI=; b=A+AaYPlv0+Eor70Y1YAZTl/9GxQMkar5pNuMFBYrcrMZ6pN8EN2OWYyUNX0FsCwcNQ JeI1n1oXafJqZv5+B4siJxqcywQaxMGqfgfVgKj21Hgnf6hNqJ4CTyTELnWp0sdYfRCu 25Ey0APQmvWztjE8xBfUfd3I7Ovr2SXZgiFnsudk4KTPzBzEFIpY3FPw/697JqQXie6y f/T+5Mg9wvOenrY8dC49g0d2UxZ70mh53l35d8mWuRmRIEvoB7zQFm9H4AjbGR9ELcWG 5dNWh9xc6O9TFaB7KPHR7pbizMW2zPQEoURixOtpJc6BfXSHiHKWrQ9oZXVAYCfsC+kI rl8Q== X-Received: by 10.180.206.70 with SMTP id lm6mr16159569wic.50.1372343265191; Thu, 27 Jun 2013 07:27:45 -0700 (PDT) MIME-Version: 1.0 Received: by 10.216.176.6 with HTTP; Thu, 27 Jun 2013 07:27:05 -0700 (PDT) In-Reply-To: References: <4ED7146272E04A47B986ED49E771E347BB4DF6F344@Ikarus.ameusgmbh.intern> Date: Thu, 27 Jun 2013 16:27:05 +0200 Message-ID: To: Florin Patan Cc: Christian Stoller , "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a11c265bed4f1a004e0239036 Subject: Re: [PHP-DEV] New syntax for multidimensional array loop with foreach From: krebs.seb@gmail.com (Sebastian Krebs) --001a11c265bed4f1a004e0239036 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable 2013/6/27 Florin Patan > On Thu, Jun 27, 2013 at 4:10 PM, 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 > > } > > > > If the keys aren't needed, you can shorten it to: > > > > $count =3D 0; > > foreach ($array as $innerArray as $value) { > > $count +=3D $value; > > } > > > > What do you think? > > > > -- > > Christian Stoller > > LEONEX Internet GmbH > > > Hi, > > > Quick question, how would the engine then treat this case: > > $array =3D array(); > $array['level1.1']['level2']['level3'] =3D 'value'; > $array['level1.2'] =3D new StdClass(); > > foreach($array as $level1 as $level2 as $level3) { ... } > When I get this right it would fail. As far as I see this is equivalent to foreach ($array as $level1) { foreach ($level1 as $level2) { foreach ($level2 as $level3) { // .. } } } So I'd say they should behave the same. I for myself find this proposal at least interesting. I don't know how many foreach-chains I see every day :( Regards, Sebastian > > > Best regards > ---- > Florin Patan > https://github.com/dlsniper > http://www.linkedin.com/in/florinpatan > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --=20 github.com/KingCrunch --001a11c265bed4f1a004e0239036--