Array decomposition, more than list()
| From: | Clint Priest | Date: | Sun, 13 May 2012 16:41:22 +0000 |
| Subject: | Array decomposition, more than list() | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-60545@lists.php.net to get a copy of this message | ||
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.
What do you think?
-Clint