PHP Array
Working with collections of data in PHP
📦 What are PHP Arrays?
Arrays store multiple values in a single variable. PHP arrays can hold different data types and are incredibly flexible for organizing related information efficiently.
<?php
// Simple array example
$fruits = array("Apple", "Banana", "Orange");
echo $fruits[0]; // Outputs: Apple
?>
Output:
Apple
Types of PHP Arrays
Indexed Arrays
Arrays with numeric indexes
<?php
$cars = array("BMW", "Audi");
echo $cars[0];
?>
Associative Arrays
Arrays with named keys
<?php
$age = array("John"=>25);
echo $age["John"];
?>
Multidimensional
Arrays containing other arrays
<?php
$matrix = array(
array(1, 2),
array(3, 4)
);
?>
Short Syntax
Modern array declaration
<?php
$colors = ["red", "blue"];
echo $colors[0];
?>
🔹 Creating Indexed Arrays
Indexed arrays use numeric indexes starting from 0. You can create them using array() function or short bracket [] syntax.
<?php
// Method 1: array() function
$fruits = array("Apple", "Banana", "Cherry");
// Method 2: Short syntax
$colors = ["Red", "Green", "Blue"];
// Accessing elements
echo $fruits[0] . "<br>"; // Apple
echo $fruits[1] . "<br>"; // Banana
echo $colors[2]; // Blue
?>
Output:
Apple
Banana
Blue
🔹 Associative Arrays
Associative arrays use named keys instead of numbers. This makes your code more readable and helps organize related data with meaningful labels.
<?php
// Create associative array
$person = array(
"name" => "John",
"age" => 30,
"city" => "New York"
);
// Access values
echo "Name: " . $person["name"] . "<br>";
echo "Age: " . $person["age"] . "<br>";
echo "City: " . $person["city"];
?>
Output:
Name: John
Age: 30
City: New York
🔹 Looping Through Arrays
Use foreach loop to iterate through array elements easily. It works with both indexed and associative arrays without manual index management.
<?php
$fruits = array("Apple", "Banana", "Orange");
// Loop through indexed array
foreach($fruits as $fruit) {
echo $fruit . "<br>";
}
echo "<br>";
// Loop through associative array
$ages = array("John"=>25, "Jane"=>30);
foreach($ages as $name => $age) {
echo "$name is $age years old<br>";
}
?>
Output:
Apple
Banana
Orange
John is 25 years old
Jane is 30 years old
🔹 Array Functions
PHP provides many built-in functions to manipulate arrays. These functions help you count, sort, search, and modify arrays efficiently.
<?php
$numbers = array(3, 1, 4, 1, 5);
// Count elements
echo "Count: " . count($numbers) . "<br>";
// Sort array
sort($numbers);
echo "Sorted: " . implode(", ", $numbers) . "<br>";
// Add element
array_push($numbers, 9);
echo "After push: " . implode(", ", $numbers) . "<br>";
// Check if value exists
if(in_array(5, $numbers)) {
echo "5 is in the array";
}
?>
Output:
Count: 5
Sorted: 1, 1, 3, 4, 5
After push: 1, 1, 3, 4, 5, 9
5 is in the array
🔹 Multidimensional Arrays
Multidimensional arrays contain one or more arrays inside them. They're useful for storing complex data structures like tables or matrices.
<?php
// 2D array
$students = array(
array("John", 25, "A"),
array("Jane", 23, "B"),
array("Bob", 24, "A")
);
// Access elements
echo $students[0][0] . " - Grade: " . $students[0][2] . "<br>";
echo $students[1][0] . " - Grade: " . $students[1][2] . "<br>";
// Loop through 2D array
foreach($students as $student) {
echo $student[0] . " is " . $student[1] . " years old<br>";
}
?>
Output:
John - Grade: A
Jane - Grade: B
John is 25 years old
Jane is 23 years old
Bob is 24 years old