3. What are arrays ?
• Arrays are data structures that contain a group of elements
that are accessed through indexes.
• Usage :
<?php
$x = array( “banana”, “orange”, “mango”, 3, 4 );
echo $x[0]; // banana
echo $x[1]; // orange
echo $x[3]; // 3
?>
4. Ways to work with arrays
<?php
$arr = array( “banana”, “orange”);
$arr[4] = “mango”;
$arr[] = “tomato”;
var_dump($arr);
?>
5. Printing Arrays
• Use print_r() or var_dump() functions to output the
contents of an array.
<?php
$arr = array( “banana”, “orange”);
var_dump($arr ); // echoes the contents of the array
?>
6. Enumerative VS. Associative
• Enumerative arrays are indexed using only numerical
indexes.
<?php
$arr = array( “banana”, “orange”);
echo $arr[0]; // banana
?>
7. Enumerative VS. Associative
• Associative arrays allow the association of an arbitrary key
to every element.
<?php
$arr = array( ‘first’ => “banana”,
‘second’ =>“orange”);
echo $arr[‘first’]; // banana
?>
8. Multidimensional Arrays
• Multidimensional arrays are arrays that contain other
arrays.
<?php
$arr = array(
array( ‘burger’, 5, 15 ),
array( ‘cola’, 2, 25 ),
array( ‘Juice’, 3, 7 ),
);
echo $arr[0][0]; // burger
?>
Title Price Quantity
Burger 5 15
Cola 2 25
Juice 3 7
10. Array Iteration
• We can use any other loop to go through arrays:
<?php
$arr1 = array(“mango" , “banana" , “tomato”);
for( $i =0; $i < count($arr1) ; $i++ ){
echo $i . “ ----- > “ . $arr1 [$i] . “<br/>”;
}
?>
11. Class Exercise
• Using arrays, write a PHP snippet that outputs the
following table data in an HTML table and calculate the
“total price” column values :
Title Price Quantity Total Price
Burger 5 10
Cola 2 4
Juice 3 7
Milk 2 6
14. Array Operations
• PHP has a vast number of functions that allow us to do
many operations on arrays.
• For a complete reference of the functions, visit
http://php.net/manual/en/ref.array.php.
16. Array Comparison
• Identical ‘===‘:
True if the keys and values are equal and are in the same
order.
<?php
$arr1 = array( “banana”, “mango”);
$arr3 = array( 1=> “mango”, 0=> “banana” );
if($arr1 === $arr3 ) // false
?>
17. Array Comparison
• array array_diff ( array $array1 , array $array2 [, array
$ ... ] ) :
Returns an array containing all the entries from array1 that
are not present in any of the other arrays.
<?php
$arr1 = array( “banana”, “mango”, “lemon”);
$arr2 = array( “banana”, “mango”);
$diff = array_diff($arr1, $arr2); // lemon
?>
18. Counting Arrays
• int count ( mixed $var [, int $mode =
COUNT_NORMAL ] ) :
Count all elements in an array.
<?php
$arr1 = array( “banana”, “mango”, “lemon”);
echo count($arr1); // 3
?>
19. Searching Arrays
• mixed array_search ( mixed $needle , array $haystack [,
bool $strict ] ) :
Searches the array for a given value and returns the
corresponding key if successful.
<?php
$arr1 = array( “banana”, “mango”, “lemon”);
echo array_search( 'mango', $arr1 ); // 1
echo array_search( ‘strawberry’, $arr1 ); // false
?>
23. Merging Arrays
• array array_merge ( array $array1 [, array $array2 [, array $... ]] )
Merges the elements of one or more arrays together so
that the values of one are appended to the end of the
previous one. It returns the resulting array.
<?php
$array1 = array( 1, 2, 3 );
$array2 = array(4, 5, 6 );
$result = array_merge($array1, $array2);
print_r($result); // 1,2,3,4,5,6
?>
24. Array Sorting
• bool sort ( array &$array [, int $sort_flags =
SORT_REGULAR ] )
This function sorts an array. Elements will be arranged from lowest to
highest when this function has completed.
<?php
$fruits = array("lemon", "orange",
"banana", "apple");
sort($fruits);
var_dump($fruits); //apple, banana, lemon, orange
?>
25. Array Sorting
• bool rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
This function sorts an array. Elements will be arranged from highest to
lowest when this function has completed.
<?php
$fruits = array("lemon", "orange",
"banana", "apple");
rsort($fruits);
var_dump($fruits); // orange, lemon, banana, apple
?>
26. Array Sorting
• bool asort ( array &$array [, int $sort_flags =
SORT_REGULAR ] )
Sorts an array from lowest to highest. This is used mainly when sorting
associative arrays.
<?php
$fruits = array( “one” => "lemon",
“two” => "orange",
“three” => "banana",
“four” => "apple");
asort($fruits);
var_dump($fruits); // four => apple, three => banana, one => lemon, two =>
orange
?>
27. Array Sorting
• bool arsort ( array &$array [, int $sort_flags =
SORT_REGULAR ] ) :
Sorts an array from highest to lowest. This is used mainly when sorting
associative arrays.
<?php
$fruits = array( “one” => "lemon",
“two” => "orange",
“three” => "banana",
“four” => "apple");
arsort($fruits);
var_dump($fruits); // two => orange, one => lemon, three =>
banana, four => apple
?>
28. Array Sorting
• bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Sorts an array by key, maintaining key to data correlations. This is
useful mainly for associative arrays.
<?php
$fruits = array("d"=>"lemon",
"a"=>"orange",
"b"=>"banana",
"c"=>"apple" );
ksort($fruits);
var_dump($fruits); // a => orange, b => banana, c => apple, d
=> lemon
?>
29. Array Sorting
• bool krsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Sorts an array by key in reverse order, maintaining key to data
correlations. This is useful mainly for associative arrays.
<?php
$fruits = array( "d"=>"lemon",
"a"=>"orange",
"b"=>"banana",
"c"=>"apple" );
krsort($fruits);
var_dump($fruits); // d => lemon, c => apple, b => banana, a =>
orange
?>
30. Assignment
• Create a PHP function that takes an array as an argument and shows its
contents one on each line. This array may contain other arrays and these
arrays may contain others, etc. The function should display all the values
of these arrays. For example :
• If the array is like this :
$array = array(
1,
“Hello”,
array( 2, 3 ,4),
array(
5,
array( 6, 7, 8)
),
“No”
); ( Continued in the next slide )