Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60552 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 88099 invoked from network); 13 May 2012 22:34:58 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 May 2012 22:34:58 -0000 Authentication-Results: pb1.pair.com header.from=cpriest@zerocue.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=cpriest@zerocue.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zerocue.com designates 74.115.204.40 as permitted sender) X-PHP-List-Original-Sender: cpriest@zerocue.com X-Host-Fingerprint: 74.115.204.40 relay-hub204.domainlocalhost.com Received: from [74.115.204.40] ([74.115.204.40:43523] helo=relay-hub204.domainlocalhost.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 49/C8-16338-F0730BF4 for ; Sun, 13 May 2012 18:34:56 -0400 Received: from MBX202.domain.local ([169.254.19.147]) by HUB204.domain.local ([192.168.69.4]) with mapi id 14.01.0355.002; Sun, 13 May 2012 18:34:52 -0400 To: "ivan.enderlin@hoa-project.net" , "internals@lists.php.net" Thread-Topic: [PHP-DEV] Array decomposition, more than list() Thread-Index: Ac0xJn+yf6T0Ob2LTKCPZ5AfUvHtogAJlzKAAAJ4SgA= Date: Sun, 13 May 2012 22:34:51 +0000 Message-ID: <9570D903A3BECE4092E924C2985CE48555C040F5@MBX202.domain.local> References: <9570D903A3BECE4092E924C2985CE48555C03DDF@MBX202.domain.local> <4FAFEB15.5040702@hoa-project.net> In-Reply-To: <4FAFEB15.5040702@hoa-project.net> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [192.168.64.26] Content-Type: multipart/alternative; boundary="_000_9570D903A3BECE4092E924C2985CE48555C040F5MBX202domainloc_" MIME-Version: 1.0 Subject: RE: [PHP-DEV] Array decomposition, more than list() From: cpriest@zerocue.com (Clint Priest) --_000_9570D903A3BECE4092E924C2985CE48555C040F5MBX202domainloc_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable In the below example, it would deconstruct as such: $tValues =3D array( 'ID1-1' =3D> array( 'ID2-1' =3D> array( 'ID3-1' =3D> 'Value1', 'ID3-2' =3D> 'Value2', ), 'ID2-2' =3D> array( 'ID3-3' =3D> 'Value3', 'ID3-4' =3D> 'Value4', ), ), ); Given this foreach line: foreach($tValues as $ID1 =3D> [$ID2 =3D> [$ID3 =3D> $Value] ]) { /* Do something */ } I would expect the log to be as: First Loop: $tValues is iterated, assigning ID1-1 to $I= D1 and array( 'ID2-1' =3D> array( 'ID3-1' =3D= > 'Value1', 'ID3-2' =3D= > 'Value2', ), 'ID2-2' =3D> array( 'ID3-3' =3D= > 'Value3', 'ID3-4' =3D= > 'Value4', ), ), to the first decomposition Which would decompose the $ID2 and its firs= t value, etc. I see what you mean by your comments, it would need to intelligently iterat= e through the lowest element arrays first on up. The above example, if iterated through, each loop I would expect to have th= ese values: Iteration ID1 ID2 ID3 Value ------------------------------------------------ 0 ID1-1 ID2-1 ID3-1 Value1 1 ID1-1 ID2-1 ID3-2 Value2 2 ID1-1 ID2-2 ID3-3 Value3 3 ID1-1 ID2-2 ID3-4 Value4 I was referring to the Array destructuring section of this document: https://developer.mozilla.org/en/JavaScript/New_in_JavaScript/1.7#Array_com= prehensions Though a great many of the other discussions on that page would be really i= ncredible for PHP as well. I see on that document it does not talk about a complex destructuring such = as I've described above. My email here was to find if this would garner any interest from the PHP co= mmunity. > -----Original Message----- > From: Ivan Enderlin @ Hoa [mailto:ivan.enderlin@hoa-project.net] > Sent: Sunday, May 13, 2012 12:11 PM > To: internals@lists.php.net > Subject: Re: [PHP-DEV] Array decomposition, more than list() > > Hi Clint, > > On 13/05/12 18:41, Clint Priest wrote: > > I've been using array decomposition in other languages such as Javascri= pt recently and find it very useful. I did not find any > conversation about it in the archives, has this topic been discussed/vett= ed/shot down already? > > > > Example use case: Indexed database results being iterated: > > > > Input: > > $tValues[$dbRow['ID1']][$dbRow['ID2']][$dbRow['ID3']] =3D > > $dbRow['Value']; > > > > Output: > > foreach($tValues as $ID1 =3D> $tSubValues1) { > > foreach($tSubValues1 as $ID2 =3D> $tSubValues2) { > > foreach($tSubValues2 as $ID3 =3D> $Value) { > > /* Do something, such as build an SQL insert */ > > } > > } > > } > > > > The above is a semi-common piece of code in our production application,= I could see list decomposition as a very convenient > alternative. > > > > New functionality would support: > > > > foreach($tValues as $ID1 =3D> [$ID2 =3D> [$ID3 =3D> $Value] ]) { > > /* Do something */ > > } > > > > The above also would indicate that associative array key decomposition = would also be allowed. I don't believe anything like the > above is possible, even outside of a foreach loop, but perhaps I am wrong= there. > The semantics of list() and foreach are different. A list() extracts data= in variables, whereas a foreach is a loop that iterates over a > structured data. Of course, I assume you know that. But what is the seman= tics you give to foreach($array as $key1 =3D> [$key2 =3D> > [$key3 =3D> $value]])? Are you interating on $key1, then $key2, then $key= 3, or just unfolding/extracting these variables? In other > terms, what happened when > $key1 has more than 1 value? Do we iterate or extract? It is not clear he= re. > Could you point out some references (e.g. from Javascript)? > > Best regards. > > -- > Ivan Enderlin > Developer of Hoa > http://hoa.42/ or http://hoa-project.net/ > > PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis) http://disc.u= niv-fcomte.fr/ and http://www.inria.fr/ > > Member of HTML and WebApps Working Group of W3C http://w3.org/ > > > > > -- > PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visi= t: http://www.php.net/unsub.php --_000_9570D903A3BECE4092E924C2985CE48555C040F5MBX202domainloc_--