0% found this document useful (0 votes)
66 views59 pages

IT ELEC1 Midterm Discussion 3

The document discusses various types of arrays in PHP including indexed arrays, associative arrays, and multidimensional arrays. It describes how to define and access elements in these different array types using functions like array() and square brackets. The document also covers sorting arrays with functions like sort(), rsort(), asort(), and ksort(), as well as getting array properties with count() and array_change_key_case().

Uploaded by

Crash Override
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views59 pages

IT ELEC1 Midterm Discussion 3

The document discusses various types of arrays in PHP including indexed arrays, associative arrays, and multidimensional arrays. It describes how to define and access elements in these different array types using functions like array() and square brackets. The document also covers sorting arrays with functions like sort(), rsort(), asort(), and ksort(), as well as getting array properties with count() and array_change_key_case().

Uploaded by

Crash Override
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 59

IT ELEC1

Midterm Discussion 3
• An array is a data structure that stores one or more similar type
of values in a single variable.

• An array is a special variable which can hold more than one


value at a time.

• Arrays are helpful to create a list of elements of similar types.

PHP Array • Arrays are commonly used in computer programs to organize


data so that a related set of values can be easily sorted or
searched.

• Arrays fall under the category of linear data structures.


• Each item stored in an array is called an element.

• Each memory location of an element in an array is identified by


a numerical index.

• In PHP, there are three types of arrays:


• Indexed Arrays
PHP Array • Associative Arrays

continue… Multidimensional Arrays

• 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 arrays are very similar to numeric arrays in terms


of functionality but they are different in terms of their index.

• 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.

• And each element in the sub-array can be an array, and so on.


• Values in the multidimensional array are accessed using multiple
index.

• In PHP, the array() function is usually used to create an array.

• Syntax of array() Function for Indexed Array:


array(value1, value2, …, valueN);
Array
• Syntax of array() Function for Associative Array:
continue… array(key1 => value1, key2 => value2, …, keyN => valueN);

• 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.

• The => is used to assign a value to an array key in associative


array.

• Defining arrays in PHP can be done in two methods.

Array • The first method in defining arrays in PHP is using the array()

continue… function.

• Examples of Defining an Indexed Array Using array()


Function:
$numArray = array(1, 2, 3, 4, 5);
$charArray = array(‘a’, ‘b’, ‘c’, ‘d’, ‘e’);
$stringArray = (“BSCS”, “BSIT”, “BSCompEng”);
• Example of Defining an Associative Array Using array()
Function:
$salaries = array(“Allan" => 25000, “Bert" => 20000,
“Carol" => 15000);

• Example of Defining a Multidimensional Array Using array()


Function:
$scores = array("Allan" => array ("physics" => 35, "math"
=> 30, "chemistry" => 39),
“Bert" => array ("physics" => 30,
Array "math" => 32, "chemistry" => 29),
“Carol" => array ("physics" => 31,
continue… "math" => 22, "chemistry" => 39));

• The second method in defining arrays in PHP is using the square


brackets.

• Example of Defining an Indexed Array Using Square Brackets:


$numArray[0] = 1;
$numArray[1] = 2;
$numArray[2] = 3;
• Example of Defining an Associative Array Using Square
Brackets:
$salaries[“Allan”] = 25000;
$salaries[“Bert”] = 20000;
$salaries[“Carol”] = 15000;

• Example of Defining a Multidimensional Array Using Square


Array Brackets:
$scores[“Allan”][“physics”] = 35;
continue… $scores[“Allan”][“math”] = 30;
$scores[“Allan”][“chemistry”] = 39;
$scores[“Bert”][“physics”] = 30;
$scores[“Bert”][“math”] = 32;
$scores[“Bert”][“chemistry”] = 29;
• The elements in an array can be sorted in alphabetical or
numerical order either ascending or descending.

• Sorting is a process of arranging items systematically according


to certain criteria.

• The sort() function sorts an indexed array in ascending order.

Sorting Arrays • Syntax of sort() Function:


sort(array, sorttype);

• The array parameter is required and specifies the array to sort.

• The sorttype parameter is optional and specifies how to


compare the array elements or items.
• Possible values for sorttype parameter:
• 0 It is the default value and equivalent to
SORT_REGULAR.
Compares items normally and don't change
types.
• 1 It is equivalent to SORT_NUMERIC.
Compares items numerically.

Sorting Arrays • 2 It is equivalent to SORT_STRING.


Compares items as strings.
continue… • 3 It is equivalent to SORT_LOCALE_STRING.
Compares items as strings, based on
current locale.
• 4 It is equivalent to SORT_NATURAL.
Compares items as strings using natural
ordering.
• Example of Using sort() Function:
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
foreach ($numbers as $a) {
echo $a . "&nbsp";
}
?>

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 . "&nbsp";
}
?>

Sorting Arrays • The asort() function sorts an associative array in ascending


continue… order according to the value.

• Syntax of asort() Function:


asort(array, sorttype);
• Example of Using asort() Function:
<?php
$age = array("Peter" => 35, "Ben" => 37, "Joe" => 43);
asort($age);
foreach ($age as $a) {
echo $a . "&nbsp";
}
?>

Sorting Arrays • The arsort() function sorts an associative array in descending order
continue… according to the value.

• Syntax of arsort() Function:


arsort(array, sorttype);
• Example of Using asort() Function:
<?php
$age = array("Peter" => 35, "Ben" => 37, "Joe" => 43);
arsort($age);
foreach ($age as $a) {
echo $a . "&nbsp";
}
?>

Sorting Arrays • The ksort() function sorts an associative array in ascending order
continue… according to the key.

• Syntax of ksort() Function:


ksort(array, sorttype);
• Example of Using asort() Function:
<?php
$age = array("Peter" => 35, "Ben" => 37, "Joe" => 43);
ksort($age);
foreach ($age as $a) {
echo $a . "&nbsp";
}
?>

Sorting Arrays • The krsort() function sorts an associative array in descending order
continue… according to the key.

• Syntax of krsort() Function:


krsort(array, sorttype);
• The count() function returns the number of elements in an
array.

• Syntax:
count(array, mode);

• The array parameter is required and specifies the array on


Getting the which the count() function counts the number of elements.

Number of • The mode parameter is optional and specifies the way of


Elements in an counting.

Array • Possible values for mode parameter:


 0 It is the default mode and does not count all
elements of multidimensional arrays.
 1 It counts the array recursively and also counts all the
elements of multidimensional arrays.
• Example of Using count() Function:
<?php
$age = array("Peter" => 35, "Ben" => 37, "Joe" => 43);
$no_of_elements = count($age);
echo $no_of_elements;
Getting the ?>

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);

• The array parameter is required and specifies the array to use.


Other Array • The case parameter is optional and specifies the letter casing.
Functions in • Possible values for case parameter:
PHP • CASE_LOWER
• It is the default value and changes the keys to
lowercase.
• CASE_UPPER
• It changes the keys to uppercase.
• Example of Using array_change_key_case() Function:
<?php
$age=array("Peter" => 35, "Ben" => 37, "Joe" => 43);
print_r(array_change_key_case($age,CASE_UPPER));
?>

• The print_r() function is an inbuilt function in PHP that is used to


Other Array print or display the information stored in a variable.

Functions in • Basically print_r() prints human-readable information about a


PHP variable regardless of the data type.

continue… • Syntax of print_r() Function:


print_r(variable, isStore);
• The variable parameter is required and specifies the variable to
be printed.

• The isStore parameter is optional and is a Boolean value that is


used to store the output of the print_r() function in a variable
rather than printing it.
Other Array
Functions in • If isStore parameter is set to false then the print_r() function

PHP will print the output inside the web browser.

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.

• The default value for isStore parameter is false.


• The array_chunk() function splits an array into chunks of new
arrays.

• 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.

• Example of Using array_chunk() Function:


<?php
Other Array $cars=array("Volvo","BMW","Toyota","Honda",
"Mercedes","Opel");
Functions in print_r(array_chunk($cars,2));
PHP ?>

continue…
• The array_column() function returns the values from a single
column in the input array.

• Syntax of array_column() Function:


array_column(array, column_key, index_key);

• The array parameter is required and specifies the multi-


Other Array dimensional array to use.

Functions in • The column_key parameter is required and it can be an integer


PHP key or a string key name of the column of values to return.

continue… • The column_key parameter can also be NULL to return


complete arrays and is useful together with index_key to re-
index the array.
• The index_key parameter is optional and specifies the column to
use as the index or key for the returned array.

• Example of Using array_column Function:


<?php
$a = array( array("id" => 5698, "first_name" =>
"Peter", "last_name" => "Griffin"),
Other Array array("id" => 4767, "first_name" => "Ben",
"last_name" => "Smith"), array("id" =>
Functions in 3809, "first_name" => "Joe",
PHP "last_name" => "Doe"));
$last_names = array_column($a, 'last_name');
continue… print_r($last_names);
?>
• The array_combine() function creates an array by using the
elements from one keys array and one values array.

• It is required that both arrays used in array_combine() function


must have equal number of elements.

• 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.

• Example of Using array_count_values() Function:


<?php
$a = array("A","Cat","Dog","A","Dog“
print_r(array_count_values($a));
Other Array ?>

Functions in • The array_diff() function compares the values of two or more


PHP arrays, and returns an array that contains the entries from first
array that are not present in second array or third array, and so
continue… on.

• Syntax of array_diff() Function:


array_diff(array1, array2, ..., arrayN);
• The array1, array2, …, arrayN parameters are required and
specifies the arrays to compare from and against to.

• Example of Using array_diffe() Function:


<?php
$a1 = array("a" => "red", "b" => "green", "c" =>
"blue", "d" => "yellow");
Other Array $a2 = array("e" => "red", "f" => "green", "g" =>
"blue");
Functions in $result = array_diff($a1, $a2);
PHP ?>
print_r($result);

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);

• The array1, array2, …, arrayN parameters are required and


specifies the arrays to compare from and against to.

• Example of Using array_diffe() Function:


Other Array <?php

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.

• The …, arrayN parameter is optional and it contains more arrays


to compare against.
• The myfunction parameter is required and it contains a string
that defines a callable comparison function.

• The comparison function must return an integer <, =, or > than


0 if the first argument is <, =, or > than the second argument.

• The array_diff_uassoc() function returns an array containing


Other Array the entries from array1 that are not present in any of the other

Functions in arrays.

PHP • The array_diff_uassoc() function was introduced in PHP 5+.

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");

PHP $result = array_diff_uassoc($a1, $a2, $a3, "myfunction");


print_r($result);

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");

PHP $result = array_diff_ukey($a1, $a2, $a3, "myfunction");


print_r($result);

continue… ?>
• The array_fill() function fills an array with values.

• Syntax:
array_fill(index, number, value);

• The index parameter is required and it contains the first index


of the returned array.
Other Array
• The number parameter is required and it specifies the number
Functions in of elements to insert.
PHP • The value parameter is required and it specifies the value to use
continue… for filling the array.

• The array_fill() function returns the filled array.

• The array_fill() function was introduced in PHP 4.2+.


• Eample:
<?php
$a1 = array_fill(3, 4, "blue");
print_r($a1);
?>

Other Array
Functions in
PHP
continue…
• The array_fill_keys() function fills an array with values,
specifying keys.

• Syntax:
array_fill_keys(keys, value);

• The keys parameter is required and it contains the array of


Other Array values that will be used as keys.

Functions in • The value parameter is required and it specifies the value to use
PHP for filling the array.

continue… • The array_fill_keys() function returns the filled array.

• The array_fill_keys() function was introduced in PHP 5.2+.


• Example:
<?php
$keys=array("a", "b", "c", "d");
$a1 = array_fill_keys($keys, "blue");
print_r($a1);
?>

Other Array
Functions in
PHP
continue…
• The array_filter() function filters the values of an array using a
callback function.

• The array_filter() function passes each value of the input array


to the callback function.

• If the callback function returns true, the current value from


Other Array input is returned into the result array.

Functions in • In array_filter() function, the array keys are preserved.


PHP • Syntax:
continue… array_filter(array, callbackfunction, flag);

• The array parameter is required and it specifies the array to


filter.
• The callbackfunction parameter is optional and it specifies the
callback function to use.

• The flag parameter is optional and it specifies what arguments


are sent to callback function.

• Possible values for flag parameter:


Other Array • ARRAY_FILTER_USE_KEY

Functions in •
It passes the key as the only argument to callback function.
ARRAY_FILTER_USE_BOTH
PHP • It passes both value and key as arguments to callback function.

continue… • The array_filter() function returns the filtered array.

• The array_filter() function was introduced in PHP 4.0.6+.


• Example:
<?php
function test_odd($var) {
return($var & 1);
}
$a1 = array(1, 3, 2, 3, 4);
print_r(array_filter($a1, "test_odd"));
Other Array ?>

Functions in
PHP
continue…
• The array_flip() function flips or exchanges all keys with their
associated values in an array.

• Syntax:
array_flip(array);

• The array parameter is required and it specifies an array of key


Other Array or value pairs to be flipped.

Functions in • The array_flip() function returns the flipped array on success.


PHP • The array_flip() function returns NULL on failure.
continue…
• The array_flip() function was introduced in PHP 4+.
• Example:
<?php
$a1 = array("a" => "red", "b" => "green",
"c" => "blue", "d" => "yellow");
$result = array_flip($a1);
print_r($result);
?>
Other Array
Functions in
PHP
continue…
• The array_intersect() function compares the 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(array1, array2, …, arrayN);

Other Array • The array_intersect() function returns an array containing the


Functions in entries from array1 that are present in all of the other arrays.

PHP • The array_intersect() function was introduced in PHP 4.0.1+.

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);

Other Array • The array_intersect_assoc() function returns an array


containing the entries from array1 that are present in all of the
Functions in other arrays.
PHP • The array_intersect_assoc() function was introduced in PHP
continue… 4.3.0+.
• Example:
<?php
$a1 = array("a" => "red", "b" => "green", "c" =>
"blue", "d" => "yellow");
$a2 = array("a" => "red", "b" => "green", "g"
=> "blue");
$a3 = array("a" => "red", "b" => "green", "g"
Other Array => "blue");
$result=array_intersect_assoc($a1,$a2,$a3);
Functions in print_r($result);
PHP ?>

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);

Other Array • The array_intersect_key() function returns an array containing


Functions in the entries from array1 that are present in all of the other
arrays.
PHP • The array_intersect_key() function was introduced in PHP
continue… 5.1.0+.
• Example:
<?php
$a1 = array("a" => "red", "b" => "green", "c" =>
"blue");
$a2 = array("c" => "yellow", "d" => "black", "e" =>
"brown");
$a3 = array("f" => "green", "c" => "purple", "g" =>
Other Array "red");
$result = array_intersect_key($a1, $a2, $a3);
Functions in print_r($result);
PHP ?>

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);

Functions in • The array_intersect_uassoc() function returns an array


PHP containing the entries from array1 that are present in all of the
other arrays.
continue…
• The array_intersect_uassoc() function was introduced in PHP
5+.
• 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" =>
Functions in "blue");
PHP $a2 = array("a" => "red", "b" => "green", "d" =>
"blue");
continue… $a3 = array("e" => "yellow", "a" => "red", "d" =>
"blue");
$result=array_intersect_uassoc($a1, $a2, $a3, "myfunction");
print_r($result);
?>
• The array_intersect_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 present in
array2, arrayN, etc.

• Syntax:
array_intersect_ukey(array1, array2, …, arrayN,
Other Array myfunction);

Functions in • The array_intersect_ukey() function returns an array containing


PHP the entries from array1 that are present in all of the other
arrays.
continue…
• The array_intersect_ukey() function was introduced in PHP
5.1.0+.
• 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" =>
Functions in "blue");
PHP $a2 = array("a" => "black", "b" => "yellow", "d"
"brown");
=>

continue… $a3 = array("e" => "purple", "f" => "white", "a"


"gold");
=>

$result = array_intersect_ukey($a1, $a2, $a3, "myfunction");


print_r($result);
?>
• The array_key_exists() function checks an array for a specified
key, and returns true if the key exists and false if the key does
not exist.

• 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.

• The array parameter is required and it specifies an array where


the key is checked.
• The array_key_exists() function returns true if the key exists.

• The array_key_exists() function returns false if the key doesn’t


exists.

• The array_key_exists() function was introduced in PHP 4.0.7+.

Other Array • Example:


Functions in <?php
$a=array("Volvo" => "XC90", "BMW" => "X5");
PHP if (array_key_exists("Volvo", $a)) {
echo "Key exists!";
continue… } else {
echo "Key does not exist!";
}
?>
• The array_keys() function returns an array containing the keys.

• Syntax:
array_keys(array, value, strict);

• The array parameter is required and it specifies an array where


the keys are retrieved from.
Other Array
• The value parameter is optional and it specifies a value where
Functions in the key with this value are returned.
PHP • The strict parameter is optional and it specifies the strictness in
continue… retrieving the key.
• Possible values for strict parameter:
• true It returns the keys with the specified value,
depending on type where numeric value is not the same as
numeric string.
• false It is the default value.
It returns the keys with the specified value,
not depending on type where numeric value is
Other Array the same as numeric string.

Functions in • The array_keys() function returns an array containing the keys.


PHP • The array_keys() function was introduced in PHP 4+.
continue…
• Example:
<?php
$a = array(10, 20, 30, "10");
print_r(array_keys($a, "10", true));
?>

Other Array
Functions in
PHP
continue…

You might also like