Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

4 PHP Array & Its Functions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CMSC 126

PHP Array

2nd semester, SY 2018-19


Wk 13

Topics
• PHP Array
• Built-in functions for Array
• Accessing the elements of an array

What is an Array?
An array is a data structure that contains a group of elements.

Typically these elements are all of the same data type such as integer.

Arrays are commonly used in computer programs to organize data so


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

1
Types of Arrays in PHP:

1. Indexed arrays - Arrays with a numeric index. The first index is 0.

Syntax: $varname = array(value1, value2, …. Valuen);


Example: $num = array(12, 32, 543,1);

To get the first element of the array, use: $num[0]

To display all elements:


for ($i=0; i<sizeof($num); $i++){
echo $num[$i];
}

Note: sizeof or count – is an array function that returns the number of elements in
the array

Types of Arrays in PHP:


2. Associative arrays - Arrays that use named keys in assigning values

$course = array(”John"=>”BSCS", ”Paul"=>”BSAM", ”Ringo"=>”BSFT”,


”George"=>”BSBio");

or:

$course[”John”] = ”BSCS”;
$course[”Paul”] = ”BSAM”;
$course[”Ringo”] = ”BSFT”;
$course[”George”] = ”BSBio”;

Types of Arrays in PHP:


2. Associative arrays - Arrays that use named keys in assigning values

To print the array elements, we will use foreach loop

Syntax: foreach($arrayname as $varname) {

}
To print the elements of array $course:

foreach($course as $c) {
echo $c;
}

2
Types of Arrays in PHP:
3. Multidimensional arrays - Arrays containing one or more arrays. PHP can
handle arrays that are five or more levels deep.
2018-08540 Alfeche, Jubille Tricia Flores Jub
2018-04719 Ardepuela, Justine Rome Apit JR
2018-10898 Bayquen, Chad Luis Bulacan Chad
2018-10785 Binobo, Christian Vincent Doctolero Chris
2018-11018 Callao, Zian Kiero Nichole Gocela Yan2
2018-05326 Coton, Jaykent Laya JK

$students = array(
array("2018-08540",” Alfeche, Jubille Tricia Flores”, “Jub”);
array("2018-04719”, "Ardepuela, Justine Rome Apit”, "JR”);
array(”2018-10898”, ” Bayquen, Chad Luis Bulacan”, ” Chad ”);
);

Types of Arrays in PHP:


3. Multidimensional arrays - Arrays containing one or more arrays. PHP can
handle arrays that are five or more levels deep.
2018-08540 Alfeche, Jubille Tricia Flores Jub
2018-04719 Ardepuela, Justine Rome Apit JR
2018-10898 Bayquen, Chad Luis Bulacan Chad
2018-10785 Binobo, Christian Vincent Doctolero Chris
2018-11018 Callao, Zian Kiero Nichole Gocela Yan2
2018-05326 Coton, Jaykent Laya JK

To access each element:

$students[0][0] = “2018-08540”;
$students[0][1] = “Alfeche, Jubille Tricia Flores”;
$students[0][2] = “Jub”;

Array Operators
The array operators are used to compare arrays.
Operator Nam e Exam ple Result
+ Union $x + $y Union of $x and $y
== Equality $x == $y True if $x and $y have the same key/value pairs

=== Identity $x === $y True if $x and $y have the same key/value pairs in
the same order and of the same types

!= Inequality $x != $y True if $x is not equal to $y

<> Inequality $x <> $y True if $x is not equal to $y

!== Non-identity $x !== $y True if $x is not identical to $y

3
Built-in functions for Array
Function Description
array() Creates an array
array_change_key_case() Changes all keys in an array to lowercase or uppercase
array_chunk() Splits an array into chunks of arrays
array_column() Returns the values from a single column in the input array
array_combine() Creates an array by using the elements from one "keys"
array and one "values" array
array_count_values() Counts all the values of an array
array_diff() Compare arrays, and returns the differences (compare
values only)
array_diff_assoc() Compare arrays, and returns the differences (compare
keys and values)
array_diff_key() Compare arrays, and returns the differences (compare
keys only)

Function Description
array_diff_uassoc() Compare arrays, and returns the differences (compare
keys and values, using a user-defined key comparison
function)
array_diff_ukey() Compare arrays, and returns the differences (compare
keys only, using a user-defined key comparison function)
array_fill() Fills an array with values
array_fill_keys() Fills an array with values, specifying keys
array_filter() Filters the values of an array using a callback function
array_flip() Flips/Exchanges all keys with their associated values in
an array
array_intersect() Compare arrays, and returns the matches (compare
values only)
array_intersect_assoc() Compare arrays and returns the matches (compare keys
and values)

Function Description
array_intersect_key() Compare arrays, and returns the matches (compare keys
only)
array_intersect_uassoc() Compare arrays, and returns the matches (compare keys
and values, using a user-defined key comparison
function)
array_intersect_ukey() Compare arrays, and returns the matches (compare keys
only, using a user-defined key comparison function)
array_key_exists() Checks if the specified key exists in the array
array_keys() Returns all the keys of an array
array_map() Sends each value of an array to a user-made function,
which returns new values
array_merge() Merges one or more arrays into one array
array_merge_recursive() Merges one or more arrays into one array recursively
array_multisort() Sorts multiple or multi-dimensional arrays

4
Function Description
array_pad() Inserts a specified number of items, with a specified
value, to an array
array_pop() Deletes the last element of an array
array_product() Calculates the product of the values in an array
array_push() Inserts one or more elements to the end of an array
array_rand() Returns one or more random keys from an array
array_reduce() Returns an array as a string, using a user-defined
function
array_replace() Replaces the values of the first array with the values
from following arrays
array_replace_recursive() Replaces the values of the first array with the values
from following arrays recursively
array_reverse() Returns an array in the reverse order
array_search() Searches an array for a given value and returns the key

Function Description
array_shift() Removes the first element from an array, and returns
the value of the removed element
array_slice() Returns selected parts of an array
array_splice() Removes and replaces specified elements of an array
array_sum() Returns the sum of the values in an array
array_udiff() Compare arrays, and returns the differences (compare
values only, using a user-defined key comparison
function)
array_udiff_assoc() Compare arrays, and returns the differences (compare
keys and values, using a built-in function to compare
the keys and a user-defined function to compare the
values)
array_udiff_uassoc() Compare arrays, and returns the differences (compare
keys and values, using two user-defined key
comparison functions)

Function Description
array_uintersect() Compare arrays, and returns the matches (compare
values only, using a user-defined key comparison
function)
array_uintersect_assoc() Compare arrays, and returns the matches (compare
keys and values, using a built-in function to compare
the keys and a user-defined function to compare the
values)
array_uintersect_uassoc() Compare arrays, and returns the matches (compare
keys and values, using two user-defined key
comparison functions)
array_unique() Removes duplicate values from an array
array_unshift() Adds one or more elements to the beginning of an array
array_values() Returns all the values of an array
array_walk() Applies a user function to every member of an array

5
Function Description
array_walk_recursive() Applies a user function recursively to every member of
an array
arsort() Sorts an associative array in descending order,
according to the value
asort() Sorts an associative array in ascending order,
according to the value
compact() Create array containing variables and their values
count() Returns the number of elements in an array
current() Returns the current element in an array
each() Returns the current key and value pair from an array
end() Sets the internal pointer of an array to its last element
extract() Imports variables into the current symbol table from an
array
in_array() Checks if a specified value exists in an array
key() Fetches a key from an array

Function Description
krsort() Sorts an associative array in descending order,
according to the key
ksort() Sorts an associative array in ascending order,
according to the key
list() Assigns variables as if they were an array
natcasesort() Sorts an array using a case insensitive "natural order"
algorithm
natsort() Sorts an array using a "natural order" algorithm
next() Advance the internal array pointer of an array
pos() Alias of current()
prev() Rewinds the internal array pointer
range() Creates an array containing a range of elements
reset() Sets the internal pointer of an array to its first element
rsort() Sorts an indexed array in descending order
shuffle() Shuffles an array

Function Description
sizeof() Alias of count()
sort() Sorts an indexed array in ascending order
uasort() Sorts an array by values using a user-defined
comparison function
uksort() Sorts an array by keys using a user-defined comparison
function
usort() Sorts an array using a user-defined comparison
function

Complete details: https://www.w3schools.com/php/php_ref_array.asp

6
Function sizeof() or count(), array_sum()

1. Functions sizeof() and count() à count the number of elements in an array, or


properties in an object.
2. Function array_sum() à returns the sum of the values in an array.
3. Function array() à creates an array.

Source Code:
<?php
$num = array(23, 43, 12, 432, 11, 22, 1, 9);
for ($i = 0; $i < sizeof($num); $i++){
echo “$num[$i] “;
}
echo "<br>Sum of all elements = ". array_sum($num);
?>

Output:
23 43 12 432 11 22 1 9
Sum of all elements = 553

Function count () and sort()


Code: Output:

<?php 2
$numbers = array(4, 6, 2, 22, 11); 4
sort($numbers); 6
11
$arrlength = count($numbers); 22
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>

Thank you!

You might also like