Define an array An array is a variable that can store a set of values of the same data type Each value in an array is termed as an element Each element of an array can be referred
Trang 1Working with Arrays
Session 13
Trang 2 Function is block of code that has
performs a specified job
Functions enhance the logical flow
Trang 3 Define an array
Create and use array
Merge array
Use single and multidimensional arrays
Use array-related functions
Trang 4Define an array
An array is a variable that can store
a set of values of the same data type
Each value in an array is termed as an element
Each element of an array can be referred by an array name and an index
An array index is used to access an element
An index can be a number or a string
Trang 6array() Function - I
values of array elements
comma to create an array
associative arrays
Trang 7array() Function - II
Syntax for creating an array using the
array() function is:
$array_name =
array([key => ] value,
[key => ] value)
Trang 8Array Identifier
element in an array
the array identifier is:
$array_name[key] = “element_value”;
Trang 9Indexed Array
integer
array, if the index type is not
specified at the time of creating
an array
integer, such as 1, 20, or 123
Trang 10Indexed Array - Example
Trang 11Associative Array
An array where the index type is string
Index value must be specified within
double quotes
Trang 12Associative Array - Example
Trang 13Merging Arrays
two or more arrays
$merged_array_name =
array_merge($first_array, $second_array);
Trang 14Merged Array - Example
Trang 15Multidimensional Arrays
Each element is an array
Each element requires an array name and
multiple set of indices
Syntax for creating a multidimensional
array is:
$array_name = array(
array(key => value), array(key => value))
Trang 16Multidimensional Array - Example
Trang 17Array-related Functions - I
sort() - Arranges the element values into
an alphabetical order
rsort() - Sorts the element values in the
descending alphabetical order
arsort() - Sorts both associative and
Trang 18Array-related Functions - II
array_flip() - Converts the element
values to index values and index values
to element values
array_intersect() - Identifies and
returns the common element value among
a group of arrays
array_keys() - Displays all the key
indices of the specified array
array_reverse() - Reverses the order of
the array elements
Trang 20rsort() Function
descending alphabetical order
Trang 22array_flip() Function
Converts the element values to index
values and index values to element values
Trang 26Summary - I
A variable can store only one value at a time
An array is a variable that can store a set
of values of the same data type
Each value in an array is termed as an
element
All the elements are referenced by a common
name
An array index is used to access an element
An index can be a number or a string
If the index is a string, the array is an
Trang 27Summary - II
We can combine the element values of two or
more arrays This process is called as merging
arrays
In a single-dimensional array, the element
includes only one level of key value pairs
In the multidimensional array, each element is
an array Each element requires an array name
and multiple set of indices
There are two types of arrays
Trang 28Summary - III
index type is string
must be specified within double quotes
store another array within it
sort()
arsort()
rsort()
array_pop()