PHP Arrays (Step-by-Step Guide with Examples)
Arrays are a fundamental part of PHP that allow you to store multiple values in a single variable. They are versatile, efficient, and widely used in PHP programming.
1. What Is an Array in PHP?
An array is a data structure that holds multiple values under a single variable. Each value in an array is assigned an index (key), making it easy to access and manipulate data.
Example: Creating an Array
<?php
$fruits = ["Apple", "Banana", "Orange"];
echo $fruits[0]; // Output: Apple
?>
2. Types of Arrays in PHP
PHP supports three types of arrays:
Array Type | Description | Example |
---|---|---|
Indexed Array | Uses numeric indexes (0, 1, 2, …) | ["Apple", "Banana", "Cherry"] |
Associative Array | Uses named keys instead of numeric indexes | ["name" => "John", "age" => 25] |
Multidimensional Array | Contains arrays inside another array | [ ["Apple", 10], ["Banana", 20] ] |
3. Indexed Arrays
Creating an Indexed Array
<?php
$colors = ["Red", "Blue", "Green"];
echo $colors[1]; // Output: Blue
?>
Adding Elements to an Array
<?php
$colors[] = "Yellow"; // Adds "Yellow" to the array
?>
Looping Through an Indexed Array
<?php
$colors = ["Red", "Blue", "Green"];
foreach ($colors as $color) {
echo $color . "<br>";
}
?>
4. Associative Arrays
Associative arrays use named keys instead of numeric indexes.
Creating an Associative Array
<?php
$person = ["name" => "John", "age" => 30, "city" => "New York"];
echo $person["name"]; // Output: John
?>
Adding New Key-Value Pairs
<?php
$person["job"] = "Developer";
?>
Looping Through an Associative Array
<?php
$person = ["name" => "John", "age" => 30, "city" => "New York"];
foreach ($person as $key => $value) {
echo "$key: $value <br>";
}
?>
5. Multidimensional Arrays
A multidimensional array is an array of arrays.
Example: 2D Array
<?php
$students = [
["Alice", 20, "A"],
["Bob", 22, "B"],
["Charlie", 21, "A"]
];
echo $students[0][0]; // Output: Alice
?>
Looping Through a Multidimensional Array
<?php
foreach ($students as $student) {
echo "Name: " . $student[0] . ", Age: " . $student[1] . ", Grade: " . $student[2] . "<br>";
}
?>
6. PHP Array Functions
PHP provides built-in functions to manipulate arrays easily.
Common Array Functions
Function | Description | Example |
---|---|---|
count() | Returns number of elements | count($arr) |
array_push() | Adds element to the end | array_push($arr, "PHP") |
array_pop() | Removes last element | array_pop($arr) |
array_merge() | Merges two arrays | array_merge($arr1, $arr2) |
in_array() | Checks if a value exists | in_array("PHP", $arr) |
array_keys() | Gets all keys from an array | array_keys($arr) |
array_values() | Gets all values from an array | array_values($arr) |
sort() | Sorts an array in ascending order | sort($arr) |
rsort() | Sorts an array in descending order | rsort($arr) |
asort() | Sorts associative array by values | asort($arr) |
ksort() | Sorts associative array by keys | ksort($arr) |
7. Sorting Arrays in PHP
Sorting Indexed Arrays
<?php
$numbers = [4, 2, 8, 1];
sort($numbers);
print_r($numbers); // Output: [1, 2, 4, 8]
?>
Sorting Associative Arrays by Value
<?php
$ages = ["John" => 30, "Alice" => 25, "Bob" => 27];
asort($ages);
print_r($ages);
?>
Sorting Associative Arrays by Key
<?php
ksort($ages);
print_r($ages);
?>
8. Converting Arrays to Strings & Vice Versa
Convert Array to String (Implode)
<?php
$colors = ["Red", "Blue", "Green"];
echo implode(", ", $colors); // Output: Red, Blue, Green
?>
Convert String to Array (Explode)
<?php
$text = "PHP, JavaScript, Python";
$languages = explode(", ", $text);
print_r($languages);
?>
9. Removing Duplicate Values from an Array
<?php
$numbers = [1, 2, 2, 3, 4, 4, 5];
$unique_numbers = array_unique($numbers);
print_r($unique_numbers);
?>
10. Filtering Arrays in PHP
You can use array_filter()
to remove elements based on conditions.
Example: Filtering Even Numbers
<?php
$numbers = [1, 2, 3, 4, 5, 6];
$evenNumbers = array_filter($numbers, function($num) {
return $num % 2 == 0;
});
print_r($evenNumbers); // Output: [2, 4, 6]
?>
PHP arrays are powerful and flexible, allowing you to store and manipulate data efficiently. Whether you use indexed, associative, or multidimensional arrays, mastering arrays will enhance your PHP skills.
💡 Next Steps:
✅ Practice using arrays in real projects
✅ Experiment with array functions
✅ **Explore advanced array operations