PHP ARRAY
INTRODUCTION
An array is a special variable, which can
hold more than one value at a time.
In PHP, the array() is used to create an
array:
ADVANTAGES USING ARRAY
Include a flexible number of list items. Can
add and delete items on the fly.
Examine each item more concisely. You can
use looping constructs in combination with
arrays to look at and operate on each array
item in a very concise manner.
Use special array operators and functions.
Built-in array operators and functions to do
things such as count the number of items,
sum the items, and sort the array.
TYPES OF ARRAYS
In PHP we have 3 types of arrays:
1. Indexed arrays - Arrays with a
numeric index
2. Associative arrays - Arrays with
named keys
3. Multidimensional arrays - Arrays
containing one or more arrays
INDEXED ARRAY
These arrays can store numbers,
strings and any object but their index
will be represented by numbers. By
default array index starts from zero.
ASSOCIATIVE ARRAYS
The associative arrays are very similar to numeric
arrays in term 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.
To store the salaries of employees in an array, a
numerically indexed array would not be the best
choice. Instead, we could use the employees names as
the keys in our associative array, and the value would
be their respective salary.
MULTIDIMENSIONAL ARRAYS
A multi-dimensional array each
element in the main array can also be
an array. And each element in the sub-
array can be an array, and so on.
Values in the multi-dimensional array
are accessed using multiple index.
BUILDING UP AN ARRAY
• You can allocate a new item in the array
and add a value at the same time using
empty square braces [] on the right hand
side of an assignment statement
$va = array();
$va[] = "Hello";
$va[] = "World";
print_r($va);
EXAMPLES OF INDEXED ARRAYS
$students = array(‘Johnson’, ‘Jackson’,’Jefferson’);
$grades = array(66, 72, 89);
echo “The first student is $student[0]”;
echo “The second student is $student[1]”;
$average = ($grades[0] + grades[1] +
grades[2])/3;
echo “The average of the grades is $average”;
$sum = 0;
for (i=0; i < count(grades); i++)
$sum += $grades[i];
USE OF “FOREACH”
foreach ($student as $item)
{
echo (“$item”);
}
Output:
Johnson Jackson Jefferson
ASSOCIATIVE ARRAYS
A string value index is used to look up or
provide a cross-reference to the data value.
Example:
$instructors = array(“Science” => “Smith”,
“Math” => “Jones”, “English” => “Jacks”);
echo “Instructor of Science is $instructors(‘Science’).”
Output:
Instructor of Science is Smith.
ASSOCIATIVE ARRAYS
oreach ($instructors as $subject => $teacher) {
echo “Subject is $subject, teacher is $teacher<br>”;
}
utput:
ubject is Science, teacher is Smith
ubject is Math, teacher is Jones
ubject is English, teacher is Jacks
ASSOCIATIVE ARRAYS
Adding an Associative Array
Item:
$instructors[“Language”] =
“Pearson”;
Deleting an Associative Array
Item:
unset($instructors[“Science”]);
Verifying an Item’s Existence:
if (isset($instructors[“Science”])) {
echo (“Science is in the list.”);
} else {
echo (“Science is NOT in the list.”);
ASSOCIATIVE ARRAYS
asort( ) : Sort an associative array
by the values of the array while
maintaining the relationship
between indices and values.
ksort( ) : Sort an associative array
by the indices of the array while
maintaining the relationship
between indices and values.
MULTIDIMENSIONAL ARRAYS
Two-dimensional is a table:
Example:
Part No. Part Name Count Price
AC10 Hammer 122 12.50
AC11 Wrench 5 5.00
$inventory = array (
‘AC10’=>array(‘part’=>’Hammer’,’Count’=>
122, ‘Price’=>12.50),
‘AC11’=>array(‘part’=>’Wrench’,’Count’=>5,
‘Price’=>5.50));
echo $inventory[‘AC10’][‘part’];
PHP ARRAY FUNCTIONS
PHP Array Functions allow you to
interact with and manipulate arrays in
various ways. PHP arrays are essential
for storing, managing, and operating on
sets of variables.
PHP supports simple and multi-
dimensional arrays and may be either
user created or created by another
function.
SORTING ARRAY
sort( $array [, $sort_flags] );
Definition and Usage
This function sorts an array. Elements will
be arranged from lowest to highest when
this function has completed.
Syntax :
1) sort (Arr_nm)
2)rsort (Arr_nm)
3)Ksort (Arr_nm)
4)krsort (Arr_nm)
ARRAY FUNCTIONS
max( ) and min( ) functions :
Determine largest and smallest
numerical value in an array,
respectively.
array_sum( ) : Sum numerical values
in the array.
sort( ) : Reorder the items in
numerical or alphabetical order.
is_array($ar) - Returns TRUE if a
variable is an array
ARRAY FUNCTIONS
array_pop( ) : Remove an item
from the end of an array.
array_push( ) : Add an item to
the end of an array.
count($ar) - How many elements in
an array
shuffle($ar) - Shuffles the array into
random order
RRAY FUNCTIONS:
array_chunk — Split an array into chunks
array_combine — Creates an array by using
one array for keys and another for its
values
array_column — Return the values from a
single column in the input array
array_count_values — Counts all the values
of an array
array_diff — Computes the difference of
arrays
array_intersect — Computes the
array_merge — Merge one or more arrays
array_pop — Pop the element off the end of array
array_push — Push one or more elements onto the
end of array
array_rand — Pick one or more random entries out of
an array
array_shift — Shift an element off the beginning of
array
array_sum — Calculate the sum of values in an array
array_search — Searches the array for a given value
and returns the corresponding key if successful
array_reverse — Return an array with elements in
reverse order
in_array — Checks if a value exists in an array
what is the difference between:
empty() ,is_null() ,isset() unset()