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/
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php