2. Understand what are arrays.
Understand php array types.
Work with array functions.
2
3. An array is like a special variable, which can accept more
than one value at a time.
Arrays are used for storing a collection of same type of
data.
Syntax:
$arrayname[key] = value;
3
4. In php there are three different kinds of arrays and each
array value is accessed using an id which is called array
index.
The following the different types of array available in
PHP.
Numeric or indexed
Associative
Multi dimensional
4
5. Numeric array - An array with a numeric index. Values
are and accessed in linear fashion
Arrays which store numbers, strings and any object but
their index will be represented by numeric.
By default index of an array starts from zero.
Example
$cars = array("Volvo", "BMW", "Toyota");
5
6. The above code creates a numeric or indexed array of strings.
It can be accessed as follows.
By giving the index position, the array elements are accessed.
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
6
7. Associative array - An stored array with strings as index.
This stores element values in association with key values
rather than in a strict linear index order.
In term of functionality the associative arrays are similar
to numeric arrays but they are different in terms of their
index.
7
8. 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.
Example
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
8
9. The above code creates an associative array. The
elements of the array’ age’ can be accessed by the key.
The following code snippet illustrates how to access the
associative array elements.
Example
$age['Peter'] = "35"; $age['Ben'] = "37"; $age['Joe'] = "43";
9
10. The print_r() variable is used for printing the values of
an array without using looping construct.
Generally arrays will be printed with the help of looping
construct to iterate through its elements and only values
of the array elements will be printed.
But this print_r() function will print both index and
value of array elements.
10
11. We know that associative arrays are in the form of key-
value pair.
Loops will print only values. print_r() function will
traverse through all elements and print the entire
content with key – value pair.
But we cannot customize the output format given by
print_r() function. The option is to choose for each loop.
This ‘for each loop’ is variation of ‘for’ loop.
11
14. Multidimensional array - An array containing one or
more arrays and values are accessed using multiple
indices.
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
14
15. $ students =array(
array(“john”, 21, “Male”);
array(“sakshi”, 20, “Female”);
);
Echo $students[1][1];
o/p: 20
Remember : The index value always starts from 0.
15
16. PHP provides pre defined functions which are useful for
working with arrays.
The following are some of the functions which are related to
arrays.
array() – creates an array.
array_merge() – Merges one or more arrays into one array.
array_pop() – Deletes the last element of an array
aray_push() - Inserts one or more elements to the end of an array
count() - Returns the length of an array.
16
18. Array-push() - Adds one or more
elements to the end of an array.
$stack = [‘a’, ‘b’];
Array_push($stack, ‘c’, ‘d’);
echo $stack;
//o/p: a, b, c, d
18
19. $array = [1, 2, 3];
$lastElement = array_pop($array);
// $lastElement is 3,
$array is now [1, 2]
19
20. $array = [1, 2, 3];
$firstElement = array_shift($array);
// $firstElement is 1,
$array is now [2, 3]
20
21. $array = [1, 2, 3];
array_unshift($array, 0);
// $array is now [0, 1, 2, 3]
21
24. $fruits = [‘apple’, ‘banana’, ‘ kiwi’];
$isbananaExists= in_array(‘banana’, $fruits);
if($isbananaExists)
Echo“ Banana is inside the list”;
else
Echo“ Banana is not in the list”;
24
25. Arrays are used for storing a collection of same type of
data.
The following the different types of array available in
PHP.
Numeric or indexed
Associative
Multi dimensional
25
26. PHP provides predefined functions for array
manipulations.
To iterate the array elements we can use print_r()
functions, for loop and foe each loop.
26