PHP – arrays: declarations, operations, sorting, and functions

Learn about arrays in PHP: declarations, operations, iterations, sorting, and built-in functions. Practice working with simple, associative, and multidimensional arrays.

Instant results from learning PHP
Chapter
6 Lessons

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() and array_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

  1. Use associative arrays instead of multiple variables with similar meaning.
  2. When iterating over large arrays, prefer foreach – it’s clear and safe.
  3. 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.

Lesson 1

Basic operations on arrays in PHP

Master PHP array operations including adding, removing, searching, sorting, and merging arrays. Complete tutorial with practical examples.

Lesson 2

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.

Lesson 3

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.

Lesson 4

Additional Array Functions in PHP

Master advanced PHP array functions including map, filter, reduce, merge, and search. Learn powerful array manipulation techniques with examples.

Lesson 5

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.

Lesson 6