> ## 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.

# PHP array

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.

```php theme={"dark"}
// 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`.

```php theme={"dark"}
$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.

```php theme={"dark"}
$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.

```php theme={"dark"}
$associative = [
    "host" => "localhost",
    "port" => 8080,
    "ssl"  => true
];
```

**3. Multidimensional Arrays**
Arrays that contain one or more arrays as values, creating a nested tree structure.

```php theme={"dark"}
$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.

```php theme={"dark"}
$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 `zval`s 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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
