Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60546 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 61667 invoked from network); 13 May 2012 17:10:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 May 2012 17:10:52 -0000 Authentication-Results: pb1.pair.com smtp.mail=ivan.enderlin@hoa-project.net; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=ivan.enderlin@hoa-project.net; sender-id=unknown Received-SPF: error (pb1.pair.com: domain hoa-project.net from 95.130.12.24 cause and error) X-PHP-List-Original-Sender: ivan.enderlin@hoa-project.net X-Host-Fingerprint: 95.130.12.24 host1.trois-doubles.net Linux 2.6 Received: from [95.130.12.24] ([95.130.12.24:42090] helo=host1.trois-doubles.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4D/D4-16338-91BEFAF4 for ; Sun, 13 May 2012 13:10:50 -0400 Received: from Hwhost2.local (62-154.77-83.cust.bluewin.ch [83.77.154.62]) by host1.trois-doubles.net (Postfix) with ESMTPA id 95CCE20087B for ; Sun, 13 May 2012 19:10:45 +0200 (CEST) Message-ID: <4FAFEB15.5040702@hoa-project.net> Date: Sun, 13 May 2012 19:10:45 +0200 Reply-To: ivan.enderlin@hoa-project.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20120510 Thunderbird/14.0a2 MIME-Version: 1.0 To: internals@lists.php.net References: <9570D903A3BECE4092E924C2985CE48555C03DDF@MBX202.domain.local> In-Reply-To: <9570D903A3BECE4092E924C2985CE48555C03DDF@MBX202.domain.local> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Array decomposition, more than list() From: ivan.enderlin@hoa-project.net ("Ivan Enderlin @ Hoa") Hi Clint, On 13/05/12 18:41, Clint Priest wrote: > I've been using array decomposition in other languages such as Javascript recently and find it very useful. I did not find any conversation about it in the archives, has this topic been discussed/vetted/shot down already? > > Example use case: Indexed database results being iterated: > > Input: > $tValues[$dbRow['ID1']][$dbRow['ID2']][$dbRow['ID3']] = $dbRow['Value']; > > Output: > foreach($tValues as $ID1 => $tSubValues1) { > foreach($tSubValues1 as $ID2 => $tSubValues2) { > foreach($tSubValues2 as $ID3 => $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 => [$ID2 => [$ID3 => $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 semantics you give to foreach($array as $key1 => [$key2 => [$key3 => $value]])? Are you interating on $key1, then $key2, then $key3, 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 here. 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.univ-fcomte.fr/ and http://www.inria.fr/ Member of HTML and WebApps Working Group of W3C http://w3.org/