Tải bản đầy đủ (.ppt) (28 trang)

Slide môn học PHP session 5 working with arrays

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (356.39 KB, 28 trang )

Working with Arrays
Session 13


Review






Function is block of code that has
performs a specified job
Functions enhance the logical flow
in a program by dividing up
complicated code sequences into
smaller modules
There are several functions:





Mathematical
String
Date and Time Functions
Error Handling Functions
PHP / Session 13 / Slide 2 of 28


Objectives




Define an array



Create and use array



Merge array



Use single and multidimensional arrays



Use array-related functions

PHP / Session 13 / Slide 3 of 28


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 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
PHP / Session 13 / Slide 4 of 28


Array
array_name[key]=“value”;



Key is referred as index
An index can be








If
an
If
an

Number
String

the index is a string, the array is
associative array
the index is a number, the array is
indexed array
PHP / Session 13 / Slide 5 of 28


array() Function - I






Create an array and set the initial
values of array elements
Uses key value pairs separated by a
comma to create an array
Can initialize both indexed and
associative arrays

PHP / Session 13 / Slide 6 of 28



array() Function - II
Syntax for creating an array using the
array() function is:

$array_name =
array([key => ] value,
[key => ] value)

PHP / Session 13 / Slide 7 of 28


Array Identifier




Initialize value of a specific
element in an array
Syntax for creating an array using
the array identifier is:
$array_name[key] = “element_value”;

PHP / Session 13 / Slide 8 of 28


Indexed Array







An array where the index type is
integer
By default, PHP creates an indexed
array, if the index type is not
specified at the time of creating
an array
Index value can start with any
integer, such as 1, 20, or 123

PHP / Session 13 / Slide 9 of 28


Indexed Array - Example
$department = array(
1 => ‘Finance’,
2 => ‘Sales’,
3 => ‘HR’,
4 => ‘Purchase’)
echo $department[1] displays:
Finance
PHP / Session 13 / Slide 10 of 28


Associative Array



An array where the index type is string



Index value must be specified within
double quotes

PHP / Session 13 / Slide 11 of 28


Associative Array - Example
$department = array(
“a” => ‘Finance’,
“b” => ‘Sales’,
“c” => ‘HR’,
“d” => ‘Purchase’)
echo $department[“c”] displays:
HR
PHP / Session 13 / Slide 12 of 28


Merging Arrays





Combine the element values of
two or more arrays
Use array_merge() function

Syntax for merging arrays is:

$merged_array_name =
array_merge($first_array, $second_array);

PHP / Session 13 / Slide 13 of 28


Merged Array - Example
$ITdept = array(
0 => “Testing”,
1 => “Training”);
$SalesPurcahsedept = array(
0 => “Advertising”,
1 => “Marketing”);

$AdminDept = array_merge($ITdept, $SalesPurchasedept)

PHP / Session 13 / Slide 14 of 28


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

PHP / Session 13 / Slide 15 of 28


Multidimensional Array - Example
$country_mdlist = array(
“USA” => array(
“Capital” => “Washington D.C.”,
“Currency” => “US Dollar”),
“England” => array(
“Capital” => “London”,
“Currency” => “Pound Sterling”),
“Australia” => array(
“Capital” => “Canberra”,
“Currency” => “Australian
Dollar”),
“New Zealand” => array(
“Capital” => “Wellington”,
“Currency” => “NZ Dollar”));

PHP / Session 13 / Slide 16 of 28



Array-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
indexed arrays
count() - Indicates the number of elements
in an array
array_count_values() - Keeps a count of
the occurrences of same element values in
an array. It returns the number of
occurrences of same element values
PHP / Session 13 / Slide 17 of 28


Array-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
PHP / Session 13 / Slide 18 of 28


sort() Function


Arranges the element values into an
alphabetical order
Key
0
1
2
3

Value

Finance
Sales
HR
Purchase

Key
0
1
2
3

Value
Finance
Sales
HR
Purchase
PHP / Session 13 / Slide 19 of 28


rsort() Function


Sorts the element values in the
descending alphabetical order
Key
0
1
2
3


Value
Finance
Sales
HR
Purchase

Key
0
1
2
3

Value
Sales
Purchase
HR
Finance
PHP / Session 13 / Slide 20 of 28


arsort() Function


Sorts both associative and indexed
arrays
Key
0
1
2
3


Value
Finance
Sales
HR
Purchase

Key
1
3
2
0

Value
Sales
Purchase
HR
Finance
PHP / Session 13 / Slide 21 of 28


array_flip() Function


Converts the element values to index
values and index values to element values

Key
0
1

2
3

Value
Finance
Sales
HR
Purchase

Key
Finance
Sales
HR
Purchase

Value
0
1
2
3
PHP / Session 13 / Slide 22 of 28


array_reverse() Function


Reverses the order of the array
elements
Key
0

1
2
3

Value
Finance
Sales
HR
Purchase

Key
0
1
2
3

Value
Purchase
HR
Sales
Finance
PHP / Session 13 / Slide 23 of 28


array_pop() Function


Pops and returns the last value of
an array
Key

0
1
2
3

Value
Finance
Sales
HR
Purchase

Key
0
1
2

Value
Finance
Sales
HR
PHP / Session 13 / Slide 24 of 28


array_push() Function


Adds one or more elements to the end
of an array
Key
0

1
2

Value
Finance
Sales
HR

Key
0
1
2
3

Value
Finance
Sales
HR
Purchase
PHP / Session 13 / Slide 25 of 28


×