Arrays are one of the most important and most frequently used data structures in PHP. With arrays, you can store many values in a single variable, manage collections of data, and process them efficiently. PHP supports three main types of arrays: indexed arrays, associative arrays, and multidimensional arrays. Mastering all three is key to writing good PHP code.
Indexed Arrays
Indexed arrays use numbers as keys, starting from 0:
<?php
// How to create an indexed array
$fruits = ['apple', 'mango', 'orange', 'banana'];
// Access an element by its index
echo $fruits[0]; // apple
echo $fruits[2]; // orange
// Add an element to the end
$fruits[] = 'watermelon';
// Iterate with foreach
foreach ($fruits as $index => $name) {
echo "$index: $name\n";
}
// Number of elements
echo count($fruits); // 5
Associative Arrays
Associative arrays use strings as keys, similar to objects/dictionaries in other languages:
<?php
// Creating an associative array
$student = [
'name' => 'Budi Santoso',
'nim' => '2021001',
'major' => 'Computer Science',
'gpa' => 3.75,
];
// Access by key
echo $student['name']; // Budi Santoso
echo $student['major']; // Computer Science
// Add or modify an element
$student['batch'] = 2021;
$student['gpa'] = 3.80; // update
// Check whether a key exists
if (isset($student['email'])) {
echo $student['email'];
} else {
echo "Email has not been set.";
}
// Iterate over key-value pairs
foreach ($student as $key => $value) {
echo "$key: $value\n";
}
Multidimensional Arrays
A multidimensional array is an array that contains other arrays as its elements. It is very useful for representing tabular data such as database query results:
<?php
// 2-dimensional array: list of students
$student_list = [
[
'name' => 'Budi Santoso',
'major' => 'Computer Science',
'gpa' => 3.75,
],
[
'name' => 'Ani Rahayu',
'major' => 'Information Systems',
'gpa' => 3.90,
],
[
'name' => 'Candra Wijaya',
'major' => 'Computer Science',
'gpa' => 3.50,
],
];
// Access a multidimensional element
echo $student_list[0]['name']; // Budi Santoso
echo $student_list[1]['gpa']; // 3.90
// Display the entire list
foreach ($student_list as $std) {
echo "{$std['name']} - {$std['major']} (GPA: {$std['gpa']})\n";
}
The Most Commonly Used Array Functions
PHP has many built-in functions for manipulating arrays. Here are the most common ones:
<?php
$numbers = [5, 2, 8, 1, 9, 3];
// Sorting an array
sort($numbers); // ascending order: [1, 2, 3, 5, 8, 9]
rsort($numbers); // descending order: [9, 8, 5, 3, 2, 1]
// For associative arrays
$scores = ['math' => 85, 'language' => 90, 'science' => 78];
asort($scores); // sort by value
ksort($scores); // sort by key
// Searching for a value
$exists = in_array(8, $numbers); // true/false
$key = array_search(8, $numbers); // index of the value 8
// Merging arrays
$a = [1, 2, 3];
$b = [4, 5, 6];
$merged = array_merge($a, $b); // [1, 2, 3, 4, 5, 6]
// Taking part of an array
$part = array_slice($numbers, 1, 3); // start at index 1, take 3 elements
// Filtering an array
$above_five = array_filter($numbers, fn($n) => $n > 5);
// Transforming an array with map
$times_two = array_map(fn($n) => $n * 2, $numbers);
// Reducing an array to a single value
$total = array_reduce($numbers, fn($carry, $item) => $carry + $item, 0);
echo $total; // sum of all numbers
Grouping Data with Multidimensional Arrays
A real-world example: grouping students by major:
<?php
$students = [
['name' => 'Budi', 'major' => 'Computer Science'],
['name' => 'Ani', 'major' => 'Information Systems'],
['name' => 'Doni', 'major' => 'Computer Science'],
['name' => 'Rina', 'major' => 'Information Systems'],
];
$groups = [];
foreach ($students as $std) {
$groups[$std['major']][] = $std['name'];
}
// Result:
// $groups['Computer Science'] = ['Budi', 'Doni']
// $groups['Information Systems'] = ['Ani', 'Rina']
foreach ($groups as $major => $list) {
echo "$major: " . implode(', ', $list) . "\n";
}
Conclusion
Arrays in PHP are extremely flexible and powerful. Indexed arrays are ideal for simple lists, associative arrays are perfect for representing objects with named properties, and multidimensional arrays are very useful for complex data such as database query results. By mastering PHP's built-in array functions such as array_filter(), array_map(), and array_reduce(), you can process data with more concise and efficient code.