Table of Contents
INCLUDE, REQUIRE, REQUIRE_ONCE FILES.................................................................................................2
PHP ARRAY..................................................................................................................................................3
DELETING ITEMS IN THE ARRAY...............................................................................................................3
ADDING ITEMS IN THE ARRAY..................................................................................................................5
ACCESSING ITEMS IN ARRAY...................................................................................................................6
ACCESSING ITEMS IN MULTI DIMENSIONAL ARRAY.................................................................................7
Example 1............................................................................................................................................7
Example 2............................................................................................................................................8
Example 3:...........................................................................................................................................9
ACCESSING AND PRINTING ALL ITEMS IN ARRAY.................................................................................10
ADDING ALL ITEMS IN ARRAY...............................................................................................................11
COMBINING TWO ARRAYS....................................................................................................................11
INCLUDE, REQUIRE, REQUIRE_ONCE FILES
Syntax:
include ‘filename’;
Examples:
include ‘connection.php’;
require ‘connect.inc.php’;
require_once ‘connect.inc.php’;
PHP ARRAY
Two ways of Creating an Array:
1. Using array() function.
2. Using []
Examples:
$students = array(“mia”, “Jhonny”, “Lexi”, 69);
$students = [“mia”, “Jhonny”, “Lexi”, 69];
DELETING ITEMS IN THE ARRAY
1. Deleting last Item in the array. Use array_pop().
$students = [“mia”, “Jhonny”, “Lexi”, 69];
array_pop($students);
//The array now is $students = [“mia”, “Jhonny”, “Lexi”];
2. Deleting specific item in the array. Use array_splice().
array_splice(array, start, length).
array_splice function has 3 parameters:
array - is the array that you want to delete the item
start - Specifies where the function will start removing elements. 0 = the first element.
Length - Specifies where the function will start removing elements. 0 = the first element
Example:
$students = [“mia”, “Jhonny”, “Lexi”, 69];
Question: Delete the second item of the array.
Answer:
array_splice($students, 1, 1);
//The array now is $students = [“mia”, “Lexi”, 69];
The first parameter is $students, this is the array that you want to delete the item.
The second parameter is 1, this the index that you want to delete. “Jhonny” is the
second item and it’s index is 1.
The third parameter is 1, this is the length of items that you want to delete. If you want
to delete two items, replace this with 2.
Here’s another example, but this time we will delete 2 items in the array.
$students = [“mia”, “Jhonny”, “Lexi”, 69];
Question. Delete the third and fourth items in the array.
Answer:
array_splice($students, 2, 2);
$students – is the array this is the array that you want to delete the item.
2 – is the index that I want to delete, because it is the third item in the array.
2 – is the length of items that I want to delete, because I want to delete 2 items; but If
you want to delete three items. Then replace it with 3.
ADDING ITEMS IN THE ARRAY
array_push is the function that add new item in the array.
Syntax:
array_push(array, item1, item2….).
Example:
1. Add “Aj Raval” to the array students.
$students = [“mia”, “Jhonny”, “Lexi”, 69];
array_push($students, “Aj Raval”);
//The array now is $students = [“mia”, “Jhonny”, “Lexi”, 69, “Aj Raval”];
2. Add “Ivana” and “Melai” to the array.
$students = [“mia”, “Jhonny”, “Lexi”, 69, “Aj Raval”];
array_push($students, “Ivani”, “Melai”);
//The array now is $students = [“mia”, “Jhonny”, “Lexi”, 69, “Aj Raval”, “Ivani”, “Melai”);];
ACCESSING ITEMS IN ARRAY
Accessing items in the array differs depending on the type of array: Indexed and Associative.
1. Indexed Array – are the default array.
$students = array(“mia”, “Jhonny”, “Lexi”, 69);
Question: Access and print “Jhonny”.
Answer: echo $students[1];
// output: Jhonny
2. Associative Array - Associative arrays are arrays that use named keys that you assign to them.
$age = [“mia” => 30, “Lexi”=> 25, “Aj” => 29];
Question: Access and print 29.
Answer: echo $age[“Aj”];
// output: 29
ACCESSING ITEMS IN MULTI DIMENSIONAL ARRAY
Multidimensional array is an array containing one or more arrays.
It has 2 dimensions: Two Dimensional Array and Three Dimensional Array.
Two Dimensional Arrays
Example 1: Pure indexed
$students = [
[“Mia”, “Lexi”, “Aj”];
[“Jorgi”, “Jhonny”];
];
Question 1: Access and Print Jhonny
Answer: echo $students[1][1]
Question 2: Access and Print Aj
Answer: echo $students[0][2]
Question 3: Access and Print Jorgi
Answer: echo $students[1][0]
Question 4: Access and Print Lexi
Answer: echo $students[0][1]
Example 2: Indexed with Associative
$students = [
[“name”=> “Mia”, “age” => 29];
[“name”=> “Lexi”, “age” => 30];
[“name”=> “Aj”, “age” => 28];
];
Question 1: Access and Print Lexi
Answer: echo $students[1][“name”];
Question 2: Access and print 30
Answer: echo $students[1][“age”];
Question 3: Access and Print Mia
Answer: echo $students[0][“name”];
Question 4: Access and Print the age Aj
Answer: echo $students[2][“name”]
Question 5: Print the sum of all the ages students
Answer: echo $students[0][“age”] + $students[1][“age”] + $students[2][“age”] ; // outputs 87
Example 3: Pure Associative
$students = [
“Fpornstars” => [“name”=> “Mia”, “age” => 29];
“Mpornstars” => [ “name” => “Jhonny”, “age” => 30 ];
];
Question 1: Access and Print Lexi
Answer: echo $students [“Fpornstars”][“name”];
Question 2: Access and Print Jhonny
Answer: echo $students [“Mpornstars”][“name”];
Question 3: Access and Print 30
Answer: echo $students [“Mpornstars”][“age”];
Question 3: Print the product of the sum of students array.
Answer: echo $students [“Fpornstars”][“age”] + [“Mpornstars”][“age”]; // outputs 59
ACCESSING AND PRINTING ALL ITEMS IN ARRAY
There are two ways to access all items in an array
1. Using foreach()
Syntax using foreach():
foreach($array_variable as $new_variable){
echo $new_variable;
Example using foreach():
1. Output all the items in the array below.
$students = [“mia”, “Jhonny”, “Lexi”, 69];
foreach($students as $student){
echo $students . ‘,’;
//output: mia, jhonny, lexi, 69;
2. Output all the items in the array below.
$students = [“mia”, “Jhonny”, “Lexi”, 69];
foreach($students as $pornstars){
echo $ pornstars. ‘<br>’;
//output:
Mia
Jhonny
Lexi
69
ADDING ALL ITEMS IN ARRAY
You can add all the items in an array using the array_sum().
$age = [50, 20, 36, 69];
echo array_sum($age);
COMBINING TWO ARRAYS
You can combine two array using array_merge().
$age = [50, 20, 36, 69];
$age2 = [24, 70, 16, 9];
$combine_age = array_merge($age, $age2);
print_r($combine_age);
// output: $combine_age = [50, 20, 36, 69, 24, 70, 16, 9];