Beruflich Dokumente
Kultur Dokumente
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
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
A <?php
R $variable_name = array(
R
A array(elements…),
Y array(elements…),
S
IN array(elements…),
P …
H
P );
?>
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];
?>
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.
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:
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)
Output:
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)