IT ELEC1 Midterm Discussion 3
IT ELEC1 Midterm Discussion 3
Midterm Discussion 3
• An array is a data structure that stores one or more similar type
of values in a single variable.
• Indexed arrays are arrays that can store numbers, strings and
any object but their index will be represented by numbers.
• In indexed arrays, the array index number starts from zero.
• Associative array will have their index as string so that you can
establish a strong association between key and values.
Array
• In multidimensional array, each element in the main array can
continue… also be an array.
• The value parameter specifies the actual value for the array element.
• The key parameter specifies the numeric or string pointer for the
array element.
• The => is the double arrow operator in PHP.
Array • The first method in defining arrays in PHP is using the array()
continue… function.
Sorting Arrays • The rsort() function sorts an indexed array in descending order.
continue… • Syntax of rsort() Function:
rsort(array, sorttype);
• Example of Using rsort() Function:
<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
foreach ($numbers as $a) {
echo $a . " ";
}
?>
Sorting Arrays • The arsort() function sorts an associative array in descending order
continue… according to the value.
Sorting Arrays • The ksort() function sorts an associative array in ascending order
continue… according to the key.
Sorting Arrays • The krsort() function sorts an associative array in descending order
continue… according to the key.
• Syntax:
count(array, mode);
Number of
Elements in an
Array
continue…
• The array_change_key_case() function changes all keys in an
array to lowercase or uppercase.
• Syntax:
array_change_key_case(array, case);
continue… • If isStore parameter is set to true then the print_r() function will
return a string containing the information which it is supposed
to print.
• Syntax:
array_chunk(array, size, preserve_key);
Other Array • The array parameter is required and specifies the array to use.
Functions in
• The size parameter is required and it is an integer that specifies
PHP the size of each chunk.
continue…
• The preserve_key parameter is optional and it is a Boolean
value that determines if the key will be preserved or not.
• Possible values for preserve_key parameter:
• true It preserves the keys.
• false It is the default value and reindexes the chunk
numerically.
continue…
• The array_column() function returns the values from a single
column in the input array.
• Syntax:
Other Array array_combine(keys, values);
Functions in • The keys parameter is required and specifies the array of keys.
PHP • The values parameter is required and specifies the array of
continue… values.
• Example of Using array_combine() Function:
<?php
$fname = array("Peter","Ben","Joe");
$age = array("35","37","43");
$c = array_combine($fname,$age);
print_r($c);
?>
Other Array
• The array_count_values() function counts all the values of an
Functions in array.
PHP • Syntax:
continue… array_count_values(array);
• The array parameter is required and specifies the array to count
values of.
continue…
• The array_diff_assoc() function compares the keys and values
of two or more arrays, and returns an array that contains the
entries from first array that are not present in second array or
third array, and so on.
• Syntax:
array_diff_assoc(array1, array2, ..., arrayN);
Functions in $a1 = array("a" => "red", "b" => "green", "c" =>
"blue", "d" => "pink");
PHP $a2 = array("a" => "red", "b" => "green", "c" =>
"blue");
continue… $result = array_diff_assoc($a1, $a2);
print_r($result);
?>
• The array_diff_key() function compares the keys of two or
more arrays and returns an array that contains the entries from
first array that are not present in second array or third array, and
so on.
• Syntax:
array_diff_key(array1, array2, ..., arrayN);
Other Array
• The array1, array2, …, arrayN parameters are required and
Functions in specifies the arrays to compare from and against to.
PHP • Example:
continue… <?php
$a1 = array("a" => "red", "b" => "green", "c"=> "blue");
$a2 = array("a"=>"red", "c" => "blue", "d" => "pink");
$result = array_diff_key($a1, $a2);
print_r($result);
?>
• The array_diff_uassoc() function compares the keys and values of
two or more arrays using a user-defined function, and return an
array that contains the entries from array1 that are not present in
array2 or arrayN, etc.
• Syntax:
array_diff_uassoc(array1, array2, …, arrayN, myfunction);
Other Array
• The array1 parameter is required and it contains the array to
Functions in compare from.
PHP • The array2 parameter is required and it contains the array to
continue… compare against.
Functions in arrays.
continue…
• Example:
<?php
function myfunction($a, $b) {
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
Other Array $a1 = array("a" => "red", "b" => "green", "c" => "blue");
Functions in $a2 = array("a" => "red", "b" => "green", "d" => "blue");
$a3 = array("e" => "yellow", "a" => "red", "d" => "blue");
continue… ?>
• The array_diff_ukey() function compares the keys of two or
more arrays using a user-defined function, and return an array
that contains the entries from array1 that are not present in
array2 or array3, etc.
• Syntax:
array_diff_ukey(array1, array2, …, arrayN, myfunction);
Other Array
Functions in • The array_diff_ukey() function returns an array containing the
PHP entries from array1 that are not present in any of the other
arrays.
continue…
• The array_diff_ukey() function was introduced in PHP 5.1+.
• Example:
<?php
function myfunction($a, $b) {
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1: -1;
}
Other Array $a1 = array("a" => "red", "b" => "green", "c" => "blue");
Functions in $a2 = array("a" => "black", "b" => "yellow", "d" => "brown");
$a3 = array("e" => "purple", "f" => "white", "a" => "gold");
continue… ?>
• The array_fill() function fills an array with values.
• Syntax:
array_fill(index, number, value);
Other Array
Functions in
PHP
continue…
• The array_fill_keys() function fills an array with values,
specifying keys.
• Syntax:
array_fill_keys(keys, value);
Functions in • The value parameter is required and it specifies the value to use
PHP for filling the array.
Other Array
Functions in
PHP
continue…
• The array_filter() function filters the values of an array using a
callback function.
Functions in
PHP
continue…
• The array_flip() function flips or exchanges all keys with their
associated values in an array.
• Syntax:
array_flip(array);
• Syntax:
array_intersect(array1, array2, …, arrayN);
continue…
• Example:
<?php
$a1 = array("a" => "red", "b" => "green",
"c" => "blue", "d" => "yellow");
$a2 = array("e" => "red", "f" => "black",
"g" => "purple");
$a3 = array("a" => "red", "b" => "black",
Other Array "h" => "yellow");
$result = array_intersect($a1, $a2, $a3);
Functions in print_r($result);
PHP ?>
continue…
• The array_intersect_assoc() function compares the keys and
values of two or more arrays, and return an array that contains
the entries from array1 that are present in array2, arrayN, etc.
• Syntax:
array_intersect_assoc(array1, array2, …, arrayN);
continue…
• The array_intersect_key() function compares the keys of two or
more arrays, and return an array that contains the entries from
array1 that are present in array2, arrayN, etc.
• Syntax:
array_intersect_key(array1, array2, …, arrayN);
continue…
• The array_intersect_uassoc() function compares the keys and
values of two or more arrays using a user-defined function, and
return an array that contains the entries from array1 that are
present in array2, arrayN, etc.
• Syntax:
array_intersect_uassoc(array1, array2, …, arrayN,
Other Array myfunction);
• Syntax:
array_intersect_ukey(array1, array2, …, arrayN,
Other Array myfunction);
• Syntax:
array_key_exists(key, array);
Other Array • The key parameter is required and it specifies the key to check
Functions in in an array.
PHP • If the key parameter is not present when you specify an array,
an integer key is generated, starting at 0 and increases by 1 for
continue… each value.
• Syntax:
array_keys(array, value, strict);
Other Array
Functions in
PHP
continue…