🌐
PHP
php.net › manual › en › language.types.array.php
PHP: Arrays - Manual
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, ...
🌐
W3Schools
w3schools.com › php › php_arrays.asp
PHP Arrays
In PHP, an array is a special variable that can hold many values under a single name, and you can access the values by referring to an index number or a name.
Discussions

What kind of array does PHP use? - Stack Overflow
The first kind is a fixed size array, for example: ... PHP uses numeric,associative arrays and multidimentional arrays. Arrays are dynamic in nature , no size should be mentioned . Go thorugh the link to find in detail . More on stackoverflow.com
🌐 stackoverflow.com
php - Type hinting - specify an array of objects - Stack Overflow
It has been made for quick'n'easy programming, and so you are not bothered with strict typing, which leaves you in dynamic type hell without any help (like a type inferencing compiler). The PHP interpreter is completely clueless about what you have put into your array, so it must iterate over ... More on stackoverflow.com
🌐 stackoverflow.com
PHP arrays aren't really arrays
The PHP ”array” is a true monster. It is the defacto substandard implementation of who knew what. The PHP array is so mindbendigly horrendeous it should be killed with hellfire. On that note PHP only has this one collection wrapper forcing you to use it. Theres is no other language that has such a badly designed array than PHP does. To be fair, PHP has no design so the way the array was made probably just was a mistake, like lots of things in PHP. More on reddit.com
🌐 r/programming
79
13
September 11, 2019
Benchmark Analysis of PHP Array Loops

Why don't you show the actual code of the 5 methods? It's really crucial. I'm especially intrigued by the foreach result, because you didn't mention reference in your 5 bullet-points list.

More on reddit.com
🌐 r/PHP
16
18
December 5, 2013
🌐
Codecademy
codecademy.com › learn › learn-php › modules › learn-php-arrays › cheatsheet
Learn PHP: Learn PHP Arrays Cheatsheet | Codecademy
The elements in an ordered array are arranged in ascending numerical order starting with zero. ... In PHP, an ordered array can be constructed with the built-in PHP function: array(). The array() function returns an array.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-arrays
PHP Arrays - GeeksforGeeks
June 2, 2025 - Arrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays.
🌐
Medium
medium.com › @deepp10342 › understand-php-arrays-f212985ec854
Understand PHP arrays. What is array ? What are the types of… | by Lovedeep Kaur | Medium
November 15, 2023 - All elements of an array are represented by an index number which starts from 0. <?php $a = array("Deep","Ram","Sham"); ?> $a[0] = Deep $a[1] = Ram $a[2] = Sham
🌐
Laravel
laravel.com › learn › php-fundamentals › arrays
Arrays | Laravel - The clean stack for Artisans and agents
In PHP, we have two main types of arrays. First, there are numeric arrays (similar to JavaScript arrays), which use numbers as their keys starting from 0. Then we have associative arrays, which use named keys (similar to JavaScript objects).
Find elsewhere
🌐
Zend
zend.com › resources › php-extensions › php-arrays
9. PHP Arrays | Zend
The “Ordered Values” part is an array of Buckets. Each Bucket contains embedded zval, string key represented by a pointer to zend_ string (it’s NULL for numeric key), and numeric key (or string hash_value for string key). Reserved space in zvlas is used to organize linked-list of colliding elements. It contains an index of the next element with the same hash_value. Historically, PHP 5 made clear distinctions between arrays and HashTable structures.
🌐
Berlinonline
berlinonline.github.io › php-introduction › chapters › arrays
Arrays — PHP Introduction — A short introduction to PHP web development
What exactly is an array? It's a data structure that represents a sequence of values of the same type, it preserves the order of its values and has a fixed length. PHP arrays don't guarantee any of that.
🌐
Phrase
support.phrase.com › hc › en-us › articles › 6111343537820--PHP-Array-Strings
.PHP - Array (Strings) – Phrase
An array is an ordered list or collection of items. The items of the array can be basically any type in PHP: a number, a string, an object, another array, etc. We often use strings as values in our locale message arrays.
🌐
Codecademy
codecademy.com › learn › learn-php-arrays-and-loops › modules › learn-php-arrays › cheatsheet
Learn PHP: Arrays and Loops: Learn PHP Arrays Cheatsheet | Codecademy
The elements in an ordered array are arranged in ascending numerical order starting with zero. ... In PHP, an ordered array can be constructed with the built-in PHP function: array(). The array() function returns an array.
🌐
W3Schools
w3schools.com › php › php_arrays_associative.asp
PHP Associative Arrays
Arrays Indexed Arrays Associative Arrays Create Arrays Access Array Items Update Array Items Add Array Items Remove Array Items Sorting Arrays Multidimensional Arrays Array Functions PHP Superglobals
🌐
SitePoint
sitepoint.com › blog › php › using php arrays: a guide for beginners
Using PHP Arrays: A Guide for Beginners — SitePoint
November 7, 2024 - PHP arrays are powerful data structures that allow developers to store and manipulate collections of values, with built-in functions for sorting, searching, filtering and transforming arrays.
🌐
Lukas Rotermund
lukasrotermund.de › posts › php-array-object-benchmarking
PHP arrays have driven me mad | Lukas Rotermund
August 30, 2025 - This is effectively what happens when you write int_array[0] for the first element of our array with the name “int_array” or int_array[1] for the second element in C. ... Due to the high level of abstraction in PHP, arrays do not behave the way you might think now.
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › php-array
What are Arrays in PHP? Everything You Should Know
January 1, 2009 - PHP Array is a data structure that stores multiple values of any type, accessed using integer indexes or string keys in associative arrays.
Top answer
1 of 6
47

If you want to ensure you are working with "Array of Foo" and you want to ensure methods receive "Array of Foo", you can:

class ArrayOfFoo extends \ArrayObject {
    public function offsetSet($key, $val) {
        if ($val instanceof Foo) {
            return parent::offsetSet($key, $val);
        }
        throw new \InvalidArgumentException('Value must be a Foo');
    }
}

then:

function workWithFoo(ArrayOfFoo $foos) {
    foreach ($foos as $foo) {
        // etc.
    }
}

$foos = new ArrayOfFoos();
$foos[] = new Foo();
workWithFoo($foos);

The secret sauce is that you're defining a new "type" of "array of foo", then passing that "type" around using type hinting protection.


The Haldayne library handles the boilerplate for membership requirement checks if you don't want to roll your own:

class ArrayOfFoo extends \Haldayne\Boost\MapOfObjects {
    protected function allowed($value) { return $value instanceof Foo; }
}

(Full-disclosure, I'm the author of Haldayne.)


Historical note: the Array Of RFC proposed this feature back in 2014. The RFC was declined with 4 yay and 16 nay. The concept recently reappeared on the internals list, but the complaints have been much the same as levied against the original RFC: adding this check would significantly affect performance.

2 of 6
26

Old post but variadic functions and array unpacking can be used (with some limitations) to accomplish typed array hinting, at least with PHP7. (I didn't test on earlier versions).

Example:

class Foo {
  public function test(){
    echo "foo";
  }   
};  

class Bar extends Foo {
  //override parent method
  public function test(){
    echo "bar";
  }   
}          

function test(Foo ...$params){
  foreach($params as $param){
    $param->test();
  }   
}   

$f = new Foo();
$b = new Bar();

$arrayOfFoo = [$f,$b];

test(...$arrayOfFoo);
//will output "foobar"


The Limitations:

  1. This isn't technically a solution, as you aren't really passing a typed array. Instead, you use the array unpacking operator1 (the "..." in the function call) to convert your array to a list of parameters, each of which must be of the type hinted in the variadic declaration2 (which also employs an ellipsis).

  2. The "..." in the function call is absolutely necessary (which isn't surprising, given the above). Trying to call

    test($arrayOfFoo)
    

    in the context of the above example will yield a type error, as the compiler expects parameter(s) of foo, not an array. See below for an, albeit hacky, solution to pass in an array of a given type directly, while preserving some type-hinting.

  3. Variadic functions may only have one variadic parameter and it must be the last parameter (since otherwise how might the compiler determine where the variadic parameter ends and the next begins) meaning you couldn't declare functions along the lines of

    function test(Foo ...$foos, Bar ...$bars){ 
        //...
    }
    

    or

    function test(Foo ...$foos, Bar $bar){
        //...
    }
    


An Only-Slightly-Better-Than-Just-Checking-Each-Element Alternative:

The following procedure is better than just checking the type of each element insofar as (1) it guarantees the parameters used in the functional body are of the correct type without cluttering the function with type checks, and (2) it throws the usual type exceptions.

Consider:

function alt(Array $foos){
    return (function(Foo ...$fooParams){

        //treat as regular function body

        foreach($fooParams as $foo){
            $foo->test();
        }

    })(...$foos);
}

The idea is define and return the result of an immediately invoked closure that takes care of all the variadic / unpacking business for you. (One could extend the principle further, defining a higher order function that generates functions of this structure, reducing boilerplate). In the above example:

alt($arrayOfFoo) // also outputs "foobar"

The issues with this approach include:

(1) Especially to inexperienced developers, it may be unclear.

(2) It may incur some performance overhead.

(3) It, much like just internally checking the array elements, treats the type checking as an implementational detail, insofar as one must inspect the function declaration (or enjoy type exceptions) to realize that only a specifically typed array is a valid parameter. In an interface or abstract function, the full type hint could not be encoded; all one could do is comment that an implementation of the above sort (or something similar) is expected.


Notes

[1]. In a nutshell: array unpacking renders equivalent

example_function($a,$b,$c);

and

example_function(...[$a,$b,$c]);


[2]. In a nutshell: variadic functions of the form

function example_function(Foo ...$bar){
    //...
}

can be validly invoked in any of the following ways:

example_function();
example_function(new Foo());
example_function(new Foo(), new Foo());
example_function(new Foo(), new Foo(), new Foo());
//and so on
🌐
Tjdraper
tjdraper.com › blog › arrays-collections-and-types-in-php
Arrays, Collections, and Types in PHP | Blog | TJDraper.com
A traditional array in software is an indexed collection of elements. And traditionally they are at least thought of as the same type of elements in any given array. Some languages don’t really enforce that they’re the same type, and some do (guess which category PHP falls into with its already non-conforming arrays).