ARRAYS
ARRAYS have similar behaviors to mathematical MATRICES, however, should not be considered as such.
This entity has common usage throughout 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 ARRAY Classes
COMMON UNIDIMENSIONAL ARRAY
KEY TYPE |
FIRST |
OTHER |
INTEGER |
ALWAYS ZERO |
POSITIVE AND SEQUENTIAL |
KEYS NOT PROVIDED |
ed48 |
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 $var1, mix $var2, ... mix $varN );
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Since PHP 5.4.0 there has been
a new way to define:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$arr_N = [ mix $var1, mix $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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
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 ];
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$arr01 = array(2, 8, 18, 32, 32, 18, 8);
$arr01 = [ 2, 8, 18, 32, 32, 18, 8 ];
$arr02 = [ 2, 8, '18', 32, 'THIRTY-TWO', "eighteen", 8 ];
$arr03 = [ 299792458, 6.67428E-11, 'c', "G" ];
$arr04 = [01023, 0B0011, 0x20b ];
?>
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 |
ed48 |
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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
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 ];
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$arr05 = array('K' => 2, "L" => 8, "M" => 18, "N" => 32, "O" => 32, "P" => 18, "Q" => 8);
$arr05 = [ 'K' => 2, "L" => 8, "M" => 18, "N" => 32, "O" => 32, "P" => 18, "Q" => 8 ];
$arr06 = [ 'THIRTY-TWO' => 32, "EIGHTEEN" => 18 ];
$arr07 = [ 'c' => 299792458, 'G' => 6.67428E-11 ];
$arr08 = [ 2 => 'two', 4 => 'four' ];
?>
COMMON MULTIDIMENSIONAL ARRAY
KEY TYPES |
FIRST |
OTHER |
INTEGER |
ALWAYS ZERO |
POSITIVE AND SEQUENTIAL |
ed48 |
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 $var1, mix $var2, ... mix $varN );
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Since PHP 5.4.0 there has been
a new way to define:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$arr_N = [ mix $var1, mix $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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
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 ];
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$arr09 = array(2, 8, 18, 32, array (32, 18, 8));
$arr09 = [ 2, 8, 18, 32, [ 32, 18, 8 ] ];
$arr10 = [ 2, 8, '18', 32, [ 'THIRTY-TWO', "eighteen", 8 ] ];
$arr11 = [ 299792458, 6.67428E-11, [ 'c', "G" ] ];
$arr12 = ['01023', '0B0011', '0x20b',
['octal', 'binary', 'hexadecimal' ] ];
$arr13 = ["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 |
ed48 |
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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
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"]];
?>