Introduction to Arrays
Arrays in PHP are used to store multiple values in a single variable. Instead of creating separate variables for each value, arrays allow you to organize related data together.
Arrays are useful when working with lists of names, numbers, products, records, and other grouped information in web applications.
Objectives
By the end of this training, you will be able to:
- Understand the concept of arrays
- Create and use arrays in PHP
- Access array elements
- Modify array values
- Use different types of arrays
- Loop through arrays
- Apply array functions in PHP
What is an Array
An array is a special variable that can hold more than one value at a time.
Example:
<?php
$colors = array("Red", "Blue", "Green");
?>
In this example, the variable $colors stores three different values.
Why Use Arrays
Arrays help developers:
- Store multiple values efficiently
- Organize data in a structured way
- Reduce repetitive variables
- Simplify data processing
- Manage dynamic web content
Types of Arrays in PHP
PHP supports three main types of arrays:
Indexed Arrays
Indexed arrays use numeric indexes.
Example:
<?php
$fruits = array("Apple", "Banana", "Mango");
echo $fruits[0];
?>
Output:
Apple
Associative Arrays
Associative arrays use named keys instead of numbers.
Example:
<?php
$student = array(
"name" => "Ali",
"age" => 20,
"city" => "Lahore"
);
echo $student["name"];
?>
Output:
Ali
Multidimensional Arrays
Multidimensional arrays contain multiple arrays inside another array.
Example:
<?php
$students = array(
array("Ali", 20),
array("Ahmed", 22),
array("Sara", 19)
);
echo $students[0][0];
?>
Output:
Ali
Accessing Array Elements
You can access array elements using indexes or keys.
Example:
<?php
$cars = array("Toyota", "Honda", "BMW");
echo $cars[1];
?>
Output:
Honda
Updating Array Values
You can modify existing array values easily.
Example:
<?php
$colors = array("Red", "Blue", "Green");
$colors[1] = "Black";
echo $colors[1];
?>
Output:
Black
Adding New Elements to Arrays
Example:
<?php
$animals = array("Cat", "Dog");
$animals[] = "Horse";
print_r($animals);
?>
Looping Through Arrays
Using For Loop
Example:
<?php
$numbers = array(10, 20, 30);
for($i = 0; $i < count($numbers); $i++) {
echo $numbers[$i];
}
?>
Using Foreach Loop
Example:
<?php
$colors = array("Red", "Blue", "Green");
foreach($colors as $color) {
echo $color;
}
?>
Common Array Functions
count()
Returns the total number of elements.
<?php
$items = array("Pen", "Book", "Bag");
echo count($items);
?>
sort()
Sorts an array in ascending order.
<?php
$numbers = array(40, 10, 30, 20);
sort($numbers);
print_r($numbers);
?>
array_push()
Adds elements to the end of an array.
<?php
$fruits = array("Apple", "Banana");
array_push($fruits, "Orange");
print_r($fruits);
?>
Real World Uses of Arrays
Arrays are commonly used in:
- Student management systems
- Shopping carts
- Product listings
- User data storage
- Website menus
- Reports and dashboards
Advantages of Arrays
- Easy data management
- Faster data processing
- Better code organization
- Reusable and scalable
- Useful for dynamic applications
Best Practices for Arrays
- Use meaningful variable names
- Choose the correct array type
- Keep arrays organized
- Avoid unnecessary nested arrays
- Use built-in PHP array functions for efficiency
Final Presentation
In your final presentation, explain:
- What arrays are in PHP
- Types of arrays
- How to create and access arrays
- Difference between indexed and associative arrays
- Array loops and functions
- Real-world applications of arrays