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 elementsarray_push($array, $value)
– add elementarray_pop($array)
– remove the last elementunset($array[$index])
– remove a selected elementin_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 ascendingrsort($array)
– sort descendingasort($array)
– sort associative array by valuesksort($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 arraysarray_keys()
andarray_values()
array_map()
– apply a function to each elementarray_filter()
– filter an arrayarray_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.
FAQ
What 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.
Chapter Lessons
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.
Basic operations on arrays in PHP
Master PHP array operations including adding, removing, searching, sorting, and merging arrays. Complete tutorial with practical examples.
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.
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.
Additional Array Functions in PHP
Master advanced PHP array functions including map, filter, reduce, merge, and search. Learn powerful array manipulation techniques with examples.
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.