Rust

array

Module

[Array]: a PHP array (an ordered map with integer and string keys).

Contents

use php_std::Array;
Rust
pub struct Array<X: Extension = Never>

A PHP array: an ordered map from ArrayKey to Value.

Packed (a Vec, no stored keys) while it is a list 0, 1, 2, ... whose next free index is its length; an ordered hash otherwise. The switch is invisible. Semantics follow the engine: assigning an existing key keeps its position, a removed key assigned again goes to the end, and push uses the next free integer key (nNextFreeElement): one past the largest integer key inserted, except that the engine's packed arrays (integer keys in ascending order, possibly with holes) set it to one past the key they append, which lowers it after trailing elements were removed ([0, 1, 2], unset 1 and 2, $a[1] = x, then $a[] uses 2). That state is tracked as the engine does (nNumUsed, nTableSize), with the table size an array of n elements is created with (max(8, 2^k >= n)).

Copy-on-write, like the engine's arrays: a clone shares the elements (an atomic reference count, so values can cross threads) and the first mutation of a shared array copies it. Mutating a clone never changes the original. A new empty array allocates nothing.

Also implements Drop, Clone, Default, IntoIterator, FromIterator, PartialEq, Debug.
use php_std::types::array::ArrayFull;
Rust
pub struct ArrayFull

$a[] = $v failed: the next integer key would be past PHP_INT_MAX ("Cannot add element to the array as the next element is already occupied").

Also implements Display.
use php_std::ArrayKey;
Rust
pub enum ArrayKey

A PHP array key: an integer or a byte string.

PHP turns canonical decimal integer strings into integer keys ($a["7"] is $a[7], "07" and "-0" stay strings): ArrayKey::normalize applies that rule. Array stores keys as given, because some producers (igbinary, object property tables) keep numeric strings as string keys.

Also implements Hash, Debug, From.
use php_std::types::array::Iter;
Rust
pub enum Iter<'a, X: Extension>

Entries of an Array in order.

Also implements Iterator, DoubleEndedIterator.
use php_std::types::array::KeyRef;
Rust
pub enum KeyRef<'a>

A borrowed ArrayKey, used to look keys up and to iterate packed arrays (which store no keys).

Also implements Hash.
use php_std::types::array::numeric_key;
Rust
pub fn numeric_key(key: &[u8]) -> Option<i64>

ZEND_HANDLE_NUMERIC_STR: the integer a string array key becomes, if any.

An optional - and decimal digits without leading zeros ("0" itself is allowed, "-0" is not), within the i64 range.