In the below example, it would deconstruct as such:
$tValues = array(
'ID1-1' => array(
'ID2-1' => array(
'ID3-1' => 'Value1',
'ID3-2' => 'Value2',
),
'ID2-2' => array(
'ID3-3' => 'Value3',
'ID3-4' => 'Value4',
),
),
);
Given this foreach line:
foreach($tValues as $ID1 => [$ID2 => [$ID3 => $Value] ]) {
/* Do something */
}
I would expect the log to be as:
First Loop:
$tValues is iterated, assigning ID1-1 to $ID1
and array(
'ID2-1' => array(
'ID3-1' =>
'Value1',
'ID3-2' =>
'Value2',
),
'ID2-2' => array(
'ID3-3' =>
'Value3',
'ID3-4' =>
'Value4',
),
), to the first decomposition
Which would decompose the $ID2 and its first
value, etc.
I see what you mean by your comments, it would need to intelligently iterate
through the lowest element arrays first on up.
The above example, if iterated through, each loop I would expect to have these
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_comprehensions
Though a great many of the other discussions on that page would be really
incredible 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
community.
> -----Original Message-----
> From: Ivan Enderlin @ Hoa [mailto:[email protected]]
> Sent: Sunday, May 13, 2012 12:11 PM
> To: [email protected]
> 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 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