Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A PHP array is a complex data structure implemented internally as an ordered map that associates values to keys. Unlike traditional C-style arrays that allocate contiguous memory blocks for elements of a single data type, PHP arrays are dynamically sized hash tables (zend_array) capable of storing heterogeneous data types. Because of this underlying implementation, a PHP array can function structurally as a vector, hash table, dictionary, collection, stack, or queue.

Syntax

Arrays are defined using either the short array syntax [] (introduced in PHP 5.4) or the legacy array() language construct.
// Short array syntax
$data = [
    "string_key" => "value",
    1            => 100,
    2            => 3.14
];

// Legacy construct
$data = array(
    "string_key" => "value",
    1            => 100,
    2            => 3.14
);

Key and Value Specifications

Values An array value can be of any valid PHP data type, including scalar types, objects, resources, or other arrays (which forms multidimensional arrays). Keys An array key must be strictly an int or a string. PHP enforces a strict set of automatic type-casting rules for keys during assignment:
  1. Strings containing valid decimal integers are cast to the int type. For example, the key "8" will be stored as 8. However, "08" will not be cast, as it is not a valid decimal integer format.
  2. Floats are cast to int by truncating the fractional part. The key 8.7 will be stored as 8.
  3. Booleans are cast to int. true becomes 1 and false becomes 0.
  4. Null is cast to an empty string "".
  5. Arrays and Objects cannot be used as keys. Attempting to do so results in an Illegal offset type warning or TypeError.
$casting_example = [
    1    => "a",
    "1"  => "b", // Overwrites key 1
    1.5  => "c", // Overwrites key 1
    true => "d", // Overwrites key 1
];
// Result: [1 => "d"]

Structural Classifications

While PHP treats all arrays as ordered maps, they are conceptually categorized by how their keys are structured: 1. Indexed Arrays Arrays where keys are omitted during declaration. PHP automatically assigns auto-incrementing integer keys, starting at 0 by default, or 1 greater than the highest previously assigned integer key.
$indexed = ["apple", "banana", "cherry"];
// Internally mapped as: [0 => "apple", 1 => "banana", 2 => "cherry"]

$mixed_index = [
    5 => "apple",
    "banana", // Assigned key 6
    "cherry"  // Assigned key 7
];
2. Associative Arrays Arrays where keys are explicitly defined, typically using strings, to create a dictionary-like structure.
$associative = [
    "host" => "localhost",
    "port" => 8080,
    "ssl"  => true
];
3. Multidimensional Arrays Arrays that contain one or more arrays as values, creating a nested tree structure.
$multidimensional = [
    "node_1" => [
        "id"     => 100,
        "active" => true
    ],
    "node_2" => [
        "id"     => 101,
        "active" => false
    ]
];

Internal Behavior and Memory

  • Order Preservation: PHP arrays maintain insertion order. When iterating over an array (e.g., via foreach), elements are traversed in the exact sequence they were added, regardless of whether the keys are sequential integers or arbitrary strings.
  • Key Overwriting: If multiple elements are declared with the same key (or keys that evaluate to the same value after casting), the last declared value overwrites the previous ones.
  • Dereferencing: Array elements are accessed or modified using square bracket syntax $array[$key]. Appending a new element without specifying a key is done using empty brackets $array[] = $value.
  • Element Removal and Gaps: Removing an element from an array (e.g., using unset()) does not re-index the array or shift subsequent elements. Because PHP arrays are hash maps rather than contiguous vectors, unsetting an element leaves a gap in the index sequence.
$sequence = ["a", "b", "c"];
unset($sequence[1]);
// Result: [0 => "a", 2 => "c"]
// The array length is 2, but the highest index remains 2.
  • Copy-on-Write (CoW): PHP arrays are assigned by value, not by reference. However, PHP optimizes memory usage via a Copy-on-Write mechanism. In modern PHP (PHP 7+), variables are represented by separate zval (Zend Value) structures. When an array is assigned to a new variable, a new zval is created, but both zvals point to the same shared, reference-counted array payload (zend_array or HashTable) in memory. The actual duplication of the array payload in memory only occurs if and when one of the variables modifies the array.
Master PHP with Deep Grasping Methodology!Learn More