PHPArrays Introduction

Arrays Introduction

An array in PHP is an ordered collection of values that all live under a single variable name. That is a deliberately loose definition, because PHP arrays are more flexible than the arrays you might know from C, Java, or Go. A PHP array is really a hybrid structure: it behaves like a numbered list when you use it that way, and like a map (a set of key/value pairs) when you use it that way instead. Under the hood, every PHP array is an ordered map — every entry has a key and a value, the entries remember the order they were inserted in, and the values do not need to share a type.

Why "ordered map" and not "list"

In many languages a list and a map are two separate data structures: a list only has positions, a map only has keys. PHP collapses both ideas into one type. When you do not supply keys yourself, PHP assigns sequential integer keys starting at 0, which is what makes an array look like a plain list. But those integer keys are not special — they are just the default. You can just as easily use strings, or a mix of both, as keys.

The same type, used two ways

PHP
<?php
$fruits = ["apple", "banana", "cherry"]; // keys 0, 1, 2 assigned automatically
$prices = ["apple" => 0.50, "banana" => 0.25]; // keys chosen explicitly

var_dump($fruits);
var_dump($prices);
array(3) {
  [0]=> string(5) "apple"
  [1]=> string(6) "banana"
  [2]=> string(6) "cherry"
}
array(2) {
  ["apple"]=> float(0.5)
  ["banana"]=> float(0.25)
}
array() versus the [] shorthand

Older PHP code creates arrays with the array() language construct. Since PHP 5.4, the short syntax [] does exactly the same thing and is now the convention almost everywhere — it is shorter to type and matches the array-access syntax you already use to read values. Both forms produce an identical array; there is no performance or behavioural difference between them.

Two ways to write the same array

PHP
<?php
$old = array(1, 2, 3);
$new = [1, 2, 3];

var_dump($old === $new); // bool(true) - identical arrays
Which one should you write?
Use `[]` in new code. You will still see `array()` in legacy codebases and in some framework documentation, so it is worth recognizing even if you never write it yourself.
Arrays can mix everything

A single PHP array can hold indexed entries and associative entries together, and any entry can itself be another array. There is no requirement that every value share a type, and no requirement that every key follow the same scheme. This flexibility is powerful, but it also means PHP will not stop you from building an array that is hard to reason about — keeping a consistent shape is a discipline you have to apply yourself.

Indexed, associative, and nested values together

PHP
<?php
$order = [
    "id" => 1042,
    "total" => 58.90,
    "paid" => true,
    "items" => ["mouse", "keyboard"], // nested indexed array
    "customer" => [                   // nested associative array
        "name" => "Priya",
        "vip" => false,
    ],
];

echo $order["customer"]["name"]; // Priya
echo "\n";
echo $order["items"][1]; // keyboard
Priya
keyboard
Counting elements with count()

count() returns how many top-level elements an array has, regardless of whether the keys are numeric, string, or mixed. It does not recurse into nested arrays by default — a nested array counts as a single element unless you explicitly pass COUNT_RECURSIVE.

Counting top-level vs recursive

PHP
<?php
$data = ["a", "b", ["c", "d", "e"]];

echo count($data);                     // 3 - the nested array is one element
echo "\n";
echo count($data, COUNT_RECURSIVE);    // 6 - 3 top-level + 3 nested
3
6
  • Arrays are the backbone of almost every real PHP program - form data, database rows, and JSON payloads all arrive as arrays.

  • Keys can be integers or strings; PHP will even convert numeric strings like "3" to the integer 3 automatically.

  • Order is preserved as written or inserted - PHP arrays are not silently reordered like a hash set might be.

  • An empty array is falsy in boolean contexts, which is handy for quick emptiness checks.

Tip
When you are not sure whether to reach for `array()` or `[]`, always pick `[]`. It is the modern standard, reads cleanly, and there is never a reason to prefer the older syntax in code you are writing today.