Sie sind auf Seite 1von 26

Aoanan, Philip Michael F.

Castañeto, Kenneth F.
What is an Array?
• An array is a special variable, which
A can hold more than one value at a time.
R
R
A • An array stores multiple values in one
Y
S single variable.
IN
P
H • An array is a data structure that stores
P
one or more similar type of values in a
single value.
Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Examples:
• Think of an array as a box of chocolates
A with slots inside. The box represents the
R
R array itself while the spaces containing
A chocolates represent the values stored
Y
S in the arrays.
IN
P
H • Egg cartons is an example of array.
P

• Also Checkerboard or Chess are


example of array. Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Three Different Kind of Arrays: 1
Numeric Arrays (Indexed Arrays)
A - An array with a numeric index.
R
R Values are stored and accessed in linear
A fashion.
Y
S - it use number as access keys. An
IN
P access key is reference to a
H memory slot in array variable. The
P
access key is used whenever we want
to read or assign a new value an
array element. Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Syntax: Numeric Arrays (Indexed Arrays)
<?php
A $variable_name[n] = value;
R
R ?>
A
Y
S or
IN
P
H <?php
P
$variable_name = array(n => value, …);
?>
Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Syntax: Numeric Arrays(Indexed Arrays)
Where,
A “$variable_name…” is the name of the
R
R variable
A “[n]” is the access index number of the
Y
S element
IN
P “value” is the value assigned to the array
H element.
P

NOTE: Built-in array functions is given in


function reference PHP Array Functions
Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Examples: Numeric Arrays(Indexed Arrays)
<?php
A
R $movie[0] = “Hello, Love, Goodbye”;
R
A $movie[1] = “Through Night and Day”;
Y $movie[2] = “Miracle in Cell No. 7”;
S
IN
$movie[3] = “Ngayong Nandito Ka”;
P $movie[4] = “One More Chance”;
H
P
echo $movie[3];
?>
Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Examples: Numeric Arrays(Indexed Arrays)
<?php
A
R $movie = array(0 => “Hello, Love, Goodbye”,
R
A 1 => “Through Night and
Y Day”,
S
IN
2 => “Miracle in Cell No. 7”,
P 3 => “Ngayong Nandito Ka”,
H
P
4 => “One More Chance”);

echo $movie[3];
?>
Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Three Different Kind of Arrays: 2
Associative Arrays
A - An array with strings as index. This
R
R stores element values in association
A with key values rather than in a
Y
S strict linear index order. It is differ
IN
P from numeric array in the sense
H that associative arrays use
P
descriptive names for id keys.
- Descriptive captions used as array
element access key. Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Syntax: Associative Arrays
<?php
A $variable_name['keyname'] = value;
R
R ?>
A
Y
S or
IN
P
H <?php
P
$variable_name = array('keyname' =>
value);
?> Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Syntax: Associative Arrays
Where,
A “$variable_name…” is the name of the
R
R variable
A “[‘keyname’]” is the access index number
Y
S of the element
IN
P “value” is the value assigned to the array
H element.
P

NOTE: Don't keep associative array inside


double quote while printing otherwise it would
not return any value. Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Examples: Associative Arrays
<?php
A
R $cast[‘Popoy’] = “John Lloyd Cruz”;
R
A $cast[‘Ethan’] = “Alden Richards”
Y $cast[‘Lito’] = “Aga Muhlach”
S
IN
$cast[‘Rocky’] = “Jericho Rosales”
P $cast[‘Ben’] = “Paolo Contis”
H
P
echo $cast[‘Popoy’];
?>
Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Examples: Associative Arrays
<?php
A
R $cast = array(‘Popoy’ => “John Lloyd Cruz”,
R
A ‘Ethan’ => “Alden Richards”,
Y ‘Lito’ => “Aga Muhlach”,
S
IN
‘Rocky’ => “Jericho Rosales”,
P ‘Ben’ => “Paolo Contis”);
H
P
echo $cast[‘Popoy’];
?>
Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Three Different Kind of Arrays: 3
Multidimensional Arrays
A - A multidimensional array is an array
R
R containing one or more arrays.
A - A multi-dimensional array each
Y
S element in the main array can also be
IN
P an array. And each element in the
H sub-array can be an array, and so on.
P
Values in the multi-dimensional array
are accessed using multiple index.
Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Syntax: Multidimensional Arrays
Two-dimensional Arrays:

A <?php
R $variable_name = array(
R
A array(elements…),
Y array(elements…),
S
IN array(elements…),
P …
H
P );
?>

Aoanan, Philip Michael F.


Castañeto, Kenneth F.
Examples: Multidimensional Arrays
Two-dimensional Arrays:
Title Price Number

rose 1.25 15

A
daisy 0.75 25

orchid 1.15 7

R
R
<?php
A
$shop = array(
Y
array("rose", 1.25 , 15),
S
IN array("daisy", 0.75 , 25),
P array("orchid", 1.15 , 7)
H );
P
echo $shop[0][0]." costs ".$shop[0][1]." and you get ".$shop[0][2]."<br />";
echo $shop[1][0]." costs ".$shop[1][1]." and you get ".$shop[1][2]."<br />";
echo $shop[2][0]." costs ".$shop[2][1]." and you get ".$shop[2][2]."<br />";
?>
Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Syntax: Multidimensional Arrays
Three-dimensional Arrays:
<?php
$variable_name = array (
A
R array (
R array (elements...),
A array (elements...),
Y ...
S ),
IN array (
P array (elements...),
H array (elements...),
P
...
),
...
);
?> Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Examples: Multidimensional Arrays
Three-dimensional Arrays:
<?php
$categories = array(
array ( array( 'CAR_TIR', 'Tires', 100 ),
A array( 'CAR_OIL', 'Oil', 10 ),
R array( 'CAR_SPK', 'Spark Plugs', 4 )
R ),
A array ( array( 'VAN_TIR', 'Tires', 120 ),
Y array( 'VAN_OIL', 'Oil', 12 ),
array( 'VAN_SPK', 'Spark Plugs', 5 )
S ),
IN array ( array( 'TRK_TIR', 'Tires', 150 ),
P array( 'TRK_OIL', 'Oil', 15 ),
H array( 'TRK_SPK', 'Spark Plugs', 6 )
P )
);

echo $categories[2][1][2];
?>

Aoanan, Philip Michael F.


Castañeto, Kenneth F.
PHP Array Operators

A
R
R
A
Y
S
IN
P The + operator returns the right-hand array appended to the left-hand
H array; for keys that exist in both arrays, the elements from the left-hand
P array will be used, and the matching elements from the right-hand
array will be ignored.

Aoanan, Philip Michael F.


Castañeto, Kenneth F.
Examples : PHP Array Operators
Union Array Operator (+)
The union operator (+) which gives the union of two arrays based on the arrays’
keys. It does loose key matching and all keys of the second array are ignored if their
A equivalent key already exists in the first array. The rest of the keys (and the
R corresponding values) of the second array are appended to the first.
R
<?php
A $a = array('one','two');$b=array('three','four','five');
Y echo '$a + $b : ';
S print_r($a+$b);
IN
P echo ”<br>”;
echo '$b + $a : ';
H print_r($b+$a);
P ?>

Output:

$a + $b : Array ( [0] => one [1] => two [2] => five )
$b + $a : Array ( [0] => three [1] => four [2] => five )
Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Examples : PHP Array Operators
Union Array Operator (+)
<?php
$a = array("a" => "apple", "b" => "banana");
A $b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
R $c = $a + $b; // Union of $a and $b
echo "Union of \$a and \$b : <br />";
R var_dump($c);
A $c = $b + $a; // Union of $b and $a
Y echo "<br />Union of \$b and \$a : <br />";
S var_dump($c);
IN ?>
P
H
P Output:

Union of $a and $b:


array(3) { ["a"]=> string(5) "apple" ["b"]=> string(6) banana" ["c"]=> string(6) "cherry" }
Union of $b and $a :
array(3) { ["a"]=> string(4) "pear" ["b"]=> string(10) "strawberry" ["c"]=> string(6) "cherry" }

Aoanan, Philip Michael F.


Castañeto, Kenneth F.
Examples : PHP Array Operators
Equality Array Operator (==)

The equality operator (==) checks whether two arrays are similar. The
A operator returns true if all key-value pairs in first array have equivalent
R key-value pairs in the second array. It matches values and keys loosely and
R the sequence of elements is ignored.
A
Y
S <?php
IN $a = array("1" => "apple", "0" => "banana");
P $b = array( "banana", "apple");
H var_dump($a == $b);
P ?>

Output:

bool(true)

Aoanan, Philip Michael F.


Castañeto, Kenneth F.
Examples : PHP Array Operators
Identity Array Operator (===)
The identity operator (===) checks if two arrays are identical.
The arrays are identical if both arrays:
have the same number of elements
A have the same key-value pairs
R have the same sequence of elements
R have the same data type of all corresponding values
A
However, for array keys, the identity operator matches loosely if a key is an integer and a
Y
similar string notation of the integer exists as a key in the other array. For a float vs. string
S key match, this operator goes strict.
IN
P <?php
H $array1 = array('0' => '0', '1' => '1', '2' => '2', '3' => '3');
P $array2 = array(0 => '0', 1 => '1', 2 => '2', 3 => '3');
var_dump($array1 === $array2);
?>

Output:

bool(true) Aoanan, Philip Michael F.


Castañeto, Kenneth F.
Examples : PHP Array Operators
Inequality Array Operator (!=) (<>)
Inequality Array Operator is used to check if both arrays have the same elements or not.

A <?php
R
R $array1 = array("Java","Smalltalk","Objective-C","Perl");
A $array2 = array("Java","Smalltalk","Objective-C","Perl");
$array3 = array("Java","Smalltalk","Objective-
Y C","Perl",".Net");
S
IN echo var_dump($array1 != $array2) ."<br>";
P echo var_dump($array2 != $array3) ."<br>";
H echo var_dump($array2 <> $array1) ."<br>";
P ?>
Output:

bool(false)
bool(true)
bool(false)
Aoanan, Philip Michael F.
Castañeto, Kenneth F.
Examples : PHP Array Operators
Non-identity Array Operator (!==)
- Two arrays are Non-identity when the Keys and Values both are not equal.

The non-identity operator checks if two arrays are not identical. Again, this operator is the
A exact opposite of the identity operator, which means this operator returns false for a
R comparison of arrays if they are identical.
R
A <?php
Y $array1 = array("PHP", "Java");
S $array2 = array("1" => "Java", "0" => "PHP");
IN
P echo var_dump($array1 != $array2) ."<br>";
H echo var_dump($array1 !== $array2) ."<br>";
?>
P
Output:

bool(false)
bool(true)

Aoanan, Philip Michael F.


Castañeto, Kenneth F.
THANK YOU FOR WATCHING AND LISTENING….

Aoanan, Philip Michael F.


Castañeto, Kenneth F.

Das könnte Ihnen auch gefallen