Foreach As List in ( PHP 5 >= 5.5.0) Tutorial

Udemy Generic 728x90

PHP 5 foreach Supports list()

Description

A new feature is added in PHP5.5 in foreach loop i.e. using list() inside foreach loop. Now Foreach loop support list() construct for unpacking nested array into separate variables.

It does not introduce any new keywords, but simply re-uses a familiar PHP construct in a new format that is easy to read.

You can break a nested array  using foreach loop into variables, and can use these variable inside loop as you need.

With list, nested array can be unpacked into variables, which improved readability. Using list inside foreach also decrease the number of lines in your code.

Syntax : Syntax of list in foreach is very easy.

<br />
$array = array(</p>
<p>array(5, 4),</p>
<p>array(2, 6)</p>
<p>);</p>
<p>foreach ($array as list($a, $b)) {</p>
<p>// Your Code here</p>
<p>}<br />

 

Parameters : There is only one parameter in this feature.

array  : –  It must be a nested array.

Examples : Check out the examples given below.

One of the simplest example of list in foreach is given below. You can use $a and $b inside foreach loop as you want.

<br />
&lt;?php</p>
<p>$array = [</p>
<p>[1, 2],</p>
<p>[3, 4],</p>
<p>];</p>
<p>foreach ($array as list($a, $b)) {</p>
<p>// $a contains the first element of the nested array,</p>
<p>// and $b contains the second element.</p>
<p>echo &quot;A: $a; B: $b\n&quot;;</p>
<p>}</p>
<p>?&gt;<br />

This will print

<br />
A: 1; B: 2</p>
<p>A: 3; B: 4<br />

Another example of list in foreach loop is given below. In this example You can pass one variable in list() if you need only first element of an array.

<br />
&lt;?php</p>
<p>$array = [</p>
<p>[1, 2],</p>
<p>[3, 4],</p>
<p>];</p>
<p>&amp;nbsp;</p>
<p>foreach ($array as list($a)) {</p>
<p>// note there is no $b here</p>
<p>echo &quot;A: $a\n&quot;;</p>
<p>}</p>
<p>?&gt;<br />

This will print

<br />
A: 1</p>
<p>A: 3<br />

In the example given below. If nested array is not of same length and you try to use more variable than array length, PHP will create a notice but it will process your whole code.

<br />
&lt;?php</p>
<p>$array = [</p>
<p>[1, 2, 3],</p>
<p>[3, 4],</p>
<p>];</p>
<p>&amp;nbsp;</p>
<p>foreach ($array as list($a, $b, $c)) {</p>
<p>echo &quot;A: $a; B : $b; C : $c\n&quot;;</p>
<p>}</p>
<p>?&gt;<br />

This will print

<br />
A: 1; B : 2; C : 3</p>
<p>PHP Notice:  Undefined offset: 2 in test.php on line 6</p>
<p>A: 3; B : 4; C :<br />

In the example given below we are going to compare this feature with and without it (foreach as list()).

<br />
&lt;?php</p>
<p>$users = array(</p>
<p>array('Sunil', 'Omrey'),</p>
<p>array('John', 'Carter'),</p>
<p>);</p>
<p>// Before this Feature</p>
<p>foreach ($users as $user) {</p>
<p>list($firstName, $lastName) = $user;</p>
<p>echo &quot;First name: $firstName, last name: $lastName. \n&quot;;</p>
<p>}</p>
<p>// After this Feature</p>
<p>foreach ($users as list($firstName, $lastName)) {</p>
<p>echo &quot;First name: $firstName, last name: $lastName.\n &quot;;</p>
<p>}</p>
<p>

This will print for both loops

<br />
First name: Sunil, last name: Omrey.</p>
<p>First name: John, last name: Carter.<br />

 

You can easily see that readability of your code is increased. Number of lines in your code is also decreased.

 

Source : http://php.net/manual/en/control-structures.foreach.php#control-structures.foreach.list

 

Udemy Generic 728x90

Spread the word. Share this post!