About this chapter
Arrays in PHP - a complete guide
Arrays in PHP are one of the most important data structures. They allow you to store multiple values in a single variable. In this chapter, you will learn how to declare arrays, perform operations, iterate, sort, and use PHP’s rich set of built-in functions.
Declaring arrays
<?php
// Indexed array
$fruits = ["apple", "banana", "orange"];
// Associative array
$user = [
"name" => "John",
"age" => 25,
"active" => true
];
// Multidimensional array
$matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
Accessing elements
echo $fruits[0]; // apple
echo $user["name"]; // John
Basic operations
-
count($array)- number of elements -
array_push($array, $value)- add element -
array_pop($array)- remove the last element -
unset($array[$index])- remove a selected element -
in_array($value, $array)- check if a value exists
Iterating over arrays
foreach ($fruits as $fruit) {
echo $fruit . "\n";
}
foreach ($user as $key => $value) {
echo "$key: $value\n";
}
Sorting arrays
-
sort($array)- sort ascending -
rsort($array)- sort descending -
asort($array)- sort associative array by values -
ksort($array)- sort associative array by keys
$numbers = [3, 1, 4, 2];
sort($numbers);
print_r($numbers); // [1, 2, 3, 4]
Useful array functions
-
array_merge()- merging arrays -
array_keys()andarray_values() -
array_map()- apply a function to each element -
array_filter()- filter an array -
array_reduce()- reduce to a single value
$nums = [1, 2, 3, 4];
$sum = array_reduce($nums, fn($carry, $item) => $carry + $item, 0);
echo $sum; // 10
Best practices
- Use associative arrays instead of multiple variables with similar meaning.
- When iterating over large arrays, prefer
foreach- it’s clear and safe. - For filtering and mapping, use built-in functions instead of writing loops manually.
FAQWhat is the difference between an indexed array and an associative array?An indexed array uses numeric indexes, while an associative array uses named keys.Does PHP have lists or dictionaries like other languages?In PHP, all such structures are implemented with arrays - they can act as lists, maps, or dictionaries.Can I mix data types in a single array?Yes, PHP allows storing different types in one array, e.g., numbers, strings, and objects.
Lessons from courses
- 1 Indexed, Associative, and Multidimensional Arrays in PHP Learn how to work with arrays in PHP. Master indexed, associative, and multidimensional arrays with practical examples and best practices.
- 2 Basic operations on arrays in PHP Master PHP array operations including adding, removing, searching, sorting, and merging arrays. Complete tutorial with practical examples.
- 3 Iterating Over Arrays in PHP: foreach, array_walk, and array_chunk Learn how to iterate through arrays in PHP using foreach, array_walk, and array_chunk. Master array looping with practical examples and best practices.
- 4 Sorting Arrays in PHP: sort, asort, ksort Learn how to sort arrays in PHP using sort, asort, and ksort functions. Master array sorting with practical examples and best practices.
- 5 Additional Array Functions in PHP Master advanced PHP array functions including map, filter, reduce, merge, and search. Learn powerful array manipulation techniques with examples.
- 6 Speed Up Array Operations in PHP: array_map, array_filter, array_walk Learn how to optimize array processing in PHP using array_map, array_filter, and array_walk. Master fast array operations with performance tips and examples.