0% found this document useful (0 votes)
2 views22 pages

Ch5 Array

Uploaded by

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

Ch5 Array

Uploaded by

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

Chapter 5

Array

By: Asst. Lect. Montadhar Moslem


Arrays
▸ What is an Array?

▸ An array stores multiple values in one single variable.

▸ An array is a special variable that can hold many values under a single name, and you can access the
values by referring to an index number or name.

2
Create Array
▸ You can create arrays by using the array() function:

▸ You can also use a shorter syntax by using the [] brackets:

▸ Multiple Lines: Line breaks are not important, so an array declaration can span multiple lines:

3
Create Array
▸ Trailing Comma: A comma after the last item ▸ Array Keys: When creating indexed arrays the
is allowed: keys are given automatically, starting at 0 and
increased by 1 for each item, so the array
above could also be created with keys:

4
Exercise

5
Array Types
▸ In PHP, there are three types of arrays:

• Indexed arrays - Arrays with a numeric index

• Associative arrays - Arrays with named keys

• Multidimensional arrays - Arrays containing one or more arrays

▸ Array Items

• Array items can be of any data type.

• The most common are strings and numbers (int, float), but array items can also be objects,
functions or even arrays.

• You can have different data types in the same array.

6
Indexed Arrays
▸ In indexed arrays each item has an index number.

▸ By default, the first item has index 0, the second item has item 1, etc.

7
Access Indexed Arrays
▸ To access an array item you can refer to the index number.

0 1 2

8
Change Value
▸ To change the value of an array item, use the index number:

0 1 2

Change the value of the second item:

9
Loop Through an Indexed Array
▸ To loop through and print all the values of an indexed array, you could use a foreach loop, like this:

10
Associative Arrays
▸ Associative arrays are arrays that use named keys that you assign to them.

11
Access Associative Arrays
▸ To access an array item you can refer to the key name.

12
Access Associative Arrays
▸ Double or Single Quotes: You can use both double and single quotes when accessing an array:

13
Change Value
▸ To change the value of an array item, use the key name:

14
Loop Through an Associative Array
▸ To loop through and print all the values of an associative array, you could use a foreach loop, like
this:

15
Exercise

16
Add Array Items
▸ To add items to an existing array, you can use the bracket [] syntax.

17
Add Array Items
▸ To add items to an associative array, or key/value array, use brackets [] for the key, and assign
value with the = operator.

18
Exercise

19
Delete Array Items
▸ Using the unset() Function

20
Delete Array Items
▸ The unset() function takes a unlimited number of arguments, and can therefore be used to delete
multiple array items:

21
Delete Array Items
▸ Remove Item From an Associative Array

▸ To remove items from an associative array, you can use the unset() function.

▸ Specify the key of the item you want to delete.

22

You might also like