ARRAY


array apg

ARRAYS have similar behaviors to mathematical MATRICES, however, should not be considered as such.


ARRAY is a type of variable commonly used in PHP.



<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
  Any ordered map associated with values and keys
 
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

?> 

 MATRICES 


Are mathematical objects, tables, arranged in rows and columns.



 ARRAYS 


Are considered ordered maps associated with values to keys.

Are optimized for different uses such as vectors, hash tables, dictionaries, collections, stacks, queues, and more.


 In this tutorial, we will study only ARRAYS 





ARRAY

In this part of the tutorial, we will only deal with the basic functions for handling ARRAYS.


However, we have a special chapter ...

... to study ARRAYS in more detail



 UNIDIMENSIONAL ARRAYS 


An UNIDIMENSIONAL ARRAY or single dimension ARRAY, is a type of linear ARRAY.

Accessing its elements involves a single subscript which can either represent a row or column index.



COMMON UNIDIMENSIONAL ARRAY
KEY TYPE FIRST OTHER
INTEGER ALWAYS ZERO POSITIVE AND SEQUENTIAL
KEYS NOT PROVIDED
VALUE TYPES FIRST OTHER
STRING ANY OF THE THREE TYPES ANY OF THE THREE TYPES
INTEGER
FLOAT
ed48

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    SOME VALID VARIABLE EXAMPLES
    COMMON UNIDIMENSIONAL
    USER-DEFINED ARRAY DEFINITION

    Before PHP 5.4.0 there was only
    one way to defining ARRAYS:
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$arr_O = array( mix $var1mix $var2, ... mix $varN );

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

    Since PHP 5.4.0 there has been 
    a new way to define:
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$arr_N = [ mix $var1mix $var2, ... mix $varN ];

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

    The two ways, then, to live together, 
    can be used interchangeably, 
    until desire to the contrary.

    The choice of which mode to use will depend,
    therefore, on the PHP version
    that will be used by the programmer.

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


?>

  1 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    SOME VALID VARIABLE EXAMPLES
    COMMON UNIDIMENSIONAL
    USER-DEFINED ARRAY DEFINITION

    $arr_O = array( mix $var1, 
                       mix $var2, ...,  
                          ... mix $varN );

    $arr_N = [ mix $var1, 
                       mix $var2, ..., 
                          ... mix $varN ];
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$arr01a = array(28183232188);

$arr01b = [ 2818323218];

$arr01c = [ 28'18'32'THIRTY-TWO'"eighteen"];

$arr01d = [ 2997924586.67428E-11'c'"G" ];

$arr01e = [010230B00110x20b ];

?>

UNIDIMENSIONAL ASSOCIATIVE ARRAY
KEY TYPES FIRST OTHER
STRING SINCE IS CONSIDERED VALID SINCE IS CONSIDERED VALID
AND NOT REPEATED
INTEGER NOT NECESSARILY
EQUIVALENT TO ZERO
DO NOT NEED TO BE SEQUENTIAL,
ONLY DIFFERENTS
ONLY ONE KEY TYPE IS ACCEPTED
VALUE TYPES FIRST OTHER
STRING ANY OF THE THREE TYPES ANY OF THE THREE TYPES
INTEGER
FLOAT
ed48

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    SOME VALID VARIABLE EXAMPLES 
    UNIDIMENSIONAL ASSOCIATIVE 
    USER-DEFINED ARRAY DEFINITION 

    Before PHP 5.4.0 there was only 
    one way to defining ARRAYS: 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$arr_O = array( mix $key1 => mix $var1
                         
mix $key2 => mix $var2
                              
mix $keyN => mix $varN );                      

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

    Since PHP 5.4.0 there has been  
    a new way to define: 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$arr_N = [ mix $key1 => mix $var1
                       
mix $key2 => mix $var2
                              
mix $keyN => mix $varN ];     

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
    The two ways, then, to live together,  
    can be used interchangeably,  
    until desire to the contrary. 

    The choice of which mode to use will depend, 
    therefore, on the PHP version 
    that will be used by the programmer. 

    In this tutorial,  
    we will always use the most 
    current version of PHP; therefore, 
    we will employ the most recent definition. 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

?>

  2 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
    SOME VALID VARIABLE EXAMPLES 
    UNIDIMENSIONAL ASSOCIATIVE
    USER-DEFINED ARRAY DEFINITION 

$arr_O = array( mix $key1 => mix $var1, 
                    mix $key2 => mix $var2, 
                     mix $keyN => mix $varN );

$arr_N = [ mix $key1 => mix $var1, 
                 mix $key2 => mix $var2, 
                mix $keyN => mix $varN ];                                   
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$arr02a = array('K' => 2
                        
"L" => 8
                        
"M" => 18
                        
"N" => 32
                        
"O" => 32
                        
"P" => 18
                        
"Q" => );

$arr02b = [ 'K' => 2
                 
"L" => 8
                 
"M" => 18
                 
"N" => 32
                 
"O" => 32
                 
"P" => 18
                 
"Q" => ];


$arr02c = [ 'THIRTY-TWO' => 32"EIGHTEEN" => 18 ];

$arr02d = [ 'c' => 299792458'G' => 6.67428E-11 ];

$arr02e = [ => 'two'=> 'four' ];

?>

 MULTIDIMENSIONAL ARRAY 


A MULTIDIMENSIONAL ARRAY is an ARRAY containing one or more ARRAYS.

PHP supports multidimensional arrays that are two, three, four, five, or more levels deep.

However, ARRAYS more than three levels deep are hard to manage for most people.



COMMON MULTIDIMENSIONAL ARRAY
KEY TYPES FIRST OTHER
INTEGER ALWAYS ZERO POSITIVE AND SEQUENTIAL
 
VALUE TYPES FIRST OTHER
STRING ANY OF THE FOUR TYPES ANY OF THE FOUR TYPES
INTEGER
FLOAT
ARRAY AT LEAST ONE MUST HAVE THIS VALUE TYPE
ed48

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    SOME VALID VARIABLE EXAMPLES
    COMMON MULTIDIMENSIONAL
    USER-DEFINED ARRAY DEFINITION

    Before PHP 5.4.0 there was only
    one way to defining ARRAYS:
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 

$arr_O = array( mix $var1mix $var2, ... mix $varN );

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    Since PHP 5.4.0 there has been 
    a new way to define:
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
   
$arr_N = [ mix $var1mix $var2, ... mix $varN ];

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    The two ways, then, to live together, 
    can be used interchangeably, 
    until desire to the contrary.

    The choice of which mode to use will depend,
    therefore, on the PHP version
    that will be used by the programmer.

    In this tutorial, 
    we will always use the most
    current version of PHP; therefore,
    we will employ the most recent definition.

    AT LEAST ONE OF THE VALUE TYPES MUST BE AN ARRAY

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

?>

  3 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    SOME VALID VARIABLE EXAMPLES
    COMMON MULTIDIMENSIONAL
    USER-DEFINED ARRAY DEFINITION

    $arr_O = array( mix $var1, mix $var2, ... mix $varN );

    $arr_N = [ mix $var1, mix $var2, ... mix $varN ];
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$arr03a = array(281832, array (32188));

$arr03b = [ 281832, [ 3218] ];


$arr03c = [ 28'18'32, [ 'THIRTY-TWO'"eighteen"] ];

$arr03d = [ 2997924586.67428E-11, [ 'c'"G" ] ];

$arr03e = ['01023''0B0011''0x20b'
              [
'octal''binary''hexadecimal' ] ];

$arr03f = ["Countries""continentes"
             [
"Brasil""Portugal""Japan"], 
             [
"South America""Europe""Asia"] ];
             
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
             
?>

ASSOCIATIVE MULTIDIMENSIONAL ARRAY
KEY TYPES FIRST OTHER
STRING SINCE IS CONSIDERED VALID SINCE IS CONSIDERED VALID
AND NOT REPEATED
INTEGER NOT NECESSARILY
EQUIVALENT TO ZERO
DO NOT NEED TO BE SEQUENTIAL,
ONLY DIFFERENTS
BOTH KEY TYPES ARE ACCEPTED
 
VALUE TYPES FIRST OTHER
STRING ANY OF THE FOUR TYPES ANY OF THE FOUR TYPES
INTEGER
FLOAT
ARRAY AT LEAST ONE MUST HAVE THIS VALUE TYPE
ed48

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    SOME VALID VARIABLE EXAMPLES
    ASSOCIATIVE MULTIDIMENSIONAL
    USER-DEFINED ARRAY DEFINITION

    Before PHP 5.4.0 there was only
    one way to defining ARRAYS:
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$arr_O = array( mix $key1 => mix $var1
                     
mix $key2 => mix $var2
                               
mix $keyN => mix $varN );

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

    Since PHP 5.4.0 there has been 
    a new way to define:
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 
$arr_N = [ mix $key1 => mix $var1
                
mix $key2 => mix $var2
                      
mix $keyN => mix $varN ];

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

    The two ways, then, to live together, 
    can be used interchangeably, 
    until desire to the contrary.

    The choice of which mode to use will depend,
    therefore, on the PHP version
    that will be used by the programmer.

    In this tutorial, 
    we will always use the most
    current version of PHP; therefore,
    we will employ the most recent definition.

    AT LEAST ONE OF THE VALUE TYPES MUST BE AN ARRAY
 
    EVENTUALLY, if a FLOAT type is used as a KEY, 
    it will be converted to INTEGER.

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

?>

  4 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    SOME VALID VARIABLE EXAMPLES
    ASSOCIATIVE MULTIDIMENSIONAL
    USER-DEFINED ARRAY DEFINITION

$arr_O = array( mix $key1 => mix $var1, 
                  mix $key2 => mix $var2, 
                       mix $keyN => mix $varN ); 
                         
$arr_N = [ mix $key1 => mix $var1, 
                  mix $key2 => mix $var2, 
                       mix $keyN => mix $varN ]; 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$arr14 = array ("Countries" => array("Brasil"
                                                         
"Portugal"
                                                         
"Japan"),
              
"Continents" => array ("South America"
                                                 
"Europe"
                                                 
"Asia"));

   
$arr14 = [ "Countries" => [ "Brasil"
                                        
"Portugal"
                                        
"Japan" ], 
              
"Continents" =>  ["South America"
                                        
"Europe"
                                        
"Asia"]];
              
$arr15 
  [ 
"Countries" => [ 'BRA' => "Brasil"
                           
'POR' => "Portugal"
                           
'JPN' => "Japan" ],  
    
"Continents" =>  ['SA' => "South America"
                              
'EU' => "Europe"
                              
'AS' => "Asia"]];
                              
//  - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
?>

 Null coalescing assignment operator 


Now in PHP 7.4 it's time to start exploring some of the new features that will be arriving alongside it.

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



  5 EXERCISE   

<?php

$str05 
'John Doe';

$arr05 = [ 'name' => 'Me''address' => 'Here' ];

$arr05['user'] ??= $str05;

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    If you run this exercise as is, 
    nothing will be displayed, 
    as there is no function to print the result.

    This will be seen shortly.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

?>

 Unpacking inside ARRAYS 


It is possible to unpack an ARRAY at a certain position in another ARRAY.



  6 EXERCISE   

<?php

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

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

$arr06 = [...$arr06a'blue''yellow''black'];

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    If you run this exercise as is, 
    nothing will be displayed, 
    as there is no function to print the result.

    This will be seen shortly.
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

?>