foreach


php128 apg

Easy way to iterate over ARRAYS.

This function works only over ARRAYS and OBJECTS.

This build has common use in PHP, being used to iterate over ARRAYS.

An error will be issued if you try to use it on a variable with a different data type or an uninitialized variable.





The behavior foreach loops are the most complex loops in PHP.

Since the PHP 7.0.0, foreach does not use the internal array pointer.

You can directly modify the elements of an ARRAY, within a loop, by assigning a value passed by reference.

In this case, in the loop, $value must be prefixed by the  &  character, however, some care must be taken, otherwise unexpected results may be obtained.

The foreach function does not support the ability to suppress error messages by using the  @  symbol.



FIRST SYNTAX TYPE


In the following, we will show the FIRST SYNTAX TYPE of -  foreach  - conditional structure:



<?php

// STRUCTURE #1

foreach ( array_expr as $value )
    
statement

?>

SECOND SYNTAX TYPE


In the following, we will show the SECOND SYNTAX TYPE of -  foreach  - conditional structure:



<?php

// STRUCTURE #2

foreach ( array_expr as $value )
    {
    
statement
    
}

?>

THIRD SYNTAX TYPE


In the following, we will show the THIRD SYNTAX TYPE of -  foreach  - conditional structure:



<?php

// STRUCTURE #3

foreach ( array_expr as $key => $value )
    
statement

?>

FOURTH SYNTAX TYPE


In the following, we will show the FOURTH SYNTAX TYPE of -  foreach  - conditional structure:



<?php

// STRUCTURE #4

foreach ( array_expr as $key => $value )
    {
    
statement
    
}

?>

  1 EXERCISE   

<?php

$ar01 
= array(=> 'Sunday'=> 'Monday'
                       
=> 'Tuesday'=> 'Wednesday'
                       
=> 'Thursday'=> 'Friday'
                       
=> 'Saturday');

foreach(
$ar01  as $vr01)
echo 
$vr01 '<br>';

?> 

 RESULT   

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday




  2 EXERCISE   

<?php

$ar02 
= [ => 'Sunday'=> 'Monday'
               
=> 'Tuesday'=> 'Wednesday'
               
=> 'Thursday'=> 'Friday'
               
=> 'Saturday'];

foreach(
$ar02  as $k02 => $vr02)
echo 
$k02 '&nbsp;&nbsp;' $vr02 '<br>';

?> 

 RESULT   

1 Sunday
2 Monday
3 Tuesday
4 Wednesday
5 Thursday
6 Friday
7 Saturday



  3 EXERCISE   

<?php

$ar03 
= ["Countries""continentes",  
             [
"Brasil""Portugal""Japan"],  
             [
"South America""Europe""Asia"] ]; 

foreach(
$ar03  as $k3 => $vr03)
echo(
$k3) , '&nbsp';

echo 
'<br><br>';

foreach(
$ar03  as $k3 => $vr03)
echo 
'<pre>';
var_dump($vr03);
echo 
'</pre>';

?> 

 RESULT   

Keys:
0 1 2 3

Values:
array(3) {
[0]=>
string(13) "South America"
[1]=>
string(6) "Europe"
[2]=>
string(4) "Asia"
}



  4 EXERCISE   

<?php

class test 

var 
$var01 'Alea jacta est'
var 
$var02 'Luck is on'

$var04 = new test

foreach(
$var04 as $key => $value)
echo 
$key ':&nbsp;' $value '<br>';

/* - - - - - - - - - - - - - - - - - - - -

   YOU MUST TRY ALSO:
   
   echo "$key => $value<br>';
   
or
    
    print "$key => $value<br>';
    
 - - - - - - - - - - - - - - - - - - - - - */
 
?> 

 RESULT   

var01: Alea jacta est

var02: Luck is on


This exercise works with a class and an object which will be studied in a special chapter.

  5 EXERCISE   

<?php

$ar05 
= [ 281832 ];

/* - - - - - - - - - - - - - - - - - - - -

   Before PHP 5.5.0, referencing $value
   is only possible if the iterated array
   can be referenced (i.e. if it is a variable).

   The following code works only as of PHP 5.5.0: 
    
 - - - - - - - - - - - - - - - - - - - - - */ 

foreach($ar05 as &$val)
{
    
$val $val 2;
}
 
   
print_r($ar05);
   echo 
'<br>';

foreach (
$ar05 as $k => $val
{         
    echo 
"<br>{$k} => {$val} &nbsp;";
    
print_r($ar05);
}

echo 
'<br><br>';

?> 

 RESULT   

Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 )

The key element = 0 is copied to the key element = 3
0 => 1 Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 1 )

The key element = 1 is copied to the key element = 3
1 => 4 Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 4 )

The key element = 2 is copied to the key element = 3
2 => 9 Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 9 )

The key element = 3 has its value repeated with
the same value as the key element = 2.
3 => 9 Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 9 )



 Null coalescing assignment operator 


As of PHP 7.4.0, we can explore some of the new features.

Here we cover the enhancements around the null coalescing operator, namely the introduction of the null coalescing assignment operator.



  6 EXERCISE   

<?php

function listarr($arr)
{
    foreach(
$arr as $k => $v)
    echo 
$k ' => ' $v '<br>';
}

$arr15a = [ 'red''green'];

$arr15b = ['blue''yellow''black'];

$arr15 = [...$arr15a'blue''yellow''black'];


listarr($arr15a);

echo 
'<br>';

listarr($arr15b);

echo 
'<br>';

listarr($arr15);

?>

 RESULT   

0 => red
1 => green

0 => blue
1 => yellow
2 => black

0 => red
1 => green
2 => blue
3 => yellow
4 => black