Introduction to Arrays in PHP | Types | Syntax – Example
In PHP, an array is essentially a map that has been arranged. A map is a type that associates values with their corresponding keys.
In programming, an array is a particular variable that may contain several values at the same time. This type is well-suited for a variety of applications; it may be used as an array, list (vector), a hash table, dictionary, collection, stack, queue, and possibly many other things.
Due to the fact that array values may be any other array, trees and multidimensional arrays are also possibilities.
Syntax
An array can be created by the array() language construct. It takes a certain number of comma-separated key => value pairs.
array( [key =>] value , ... ) // key may be an integer or string // value may be any value <?php $arr = array("foo" => "bar", 12 => true); echo $arr["foo"]; // bar echo $arr[12]; // 1 ?>
A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e., “8” will be interpreted as 8, while “08” will be interpreted as “08”). There are no different indexed and associative array types in PHP; there is only one array type, which can both contain integer and string indices.
A value can be of any PHP type.
<?php $arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42)); echo $arr["somearray"][6]; // 5 echo $arr["somearray"][13]; // 9 echo $arr["somearray"]["a"]; // 42 ?>
If you do not specify a key for a given value, then the maximum of the integer indices is taken, and the new key will be that maximum value + 1. If you specify a key that already has a value assigned to it, that value will be overwritten.
Also read: What is a String in C++? Working | Complete Details
<?php // This array is the same as ... array(5 => 43, 32, 56, "b" => 12); // ...this array array(5 => 43, 6 => 32, 7 => 56, "b" => 12); ?>
Using TRUE as a key will evaluate integer 1 as key. Using FALSE as a key will evaluate integer 0 as key. Using NULL as a key will evaluate the empty string. Using the empty string as the key will create (or overwrite) a key with the empty string and its value; it is not the same as using empty brackets. You cannot use arrays or objects as keys. Doing so will result in a warning: Illegal offset type.
Types of Arrays in PHP
In PHP, there are three types of arrays:
Numeric arrays:
A numeric array is a data structure that allows for storing multiple values in a single variable. It is an ordered collection of elements, each identified by an index number. With PHP’s built-in array functions and operators, programmers can easily manipulate and access the elements within a numeric array.
Numeric arrays in PHP are incredibly versatile as they can hold any type of data, including integers, floats, strings, and even other arrays.
The flexibility of numeric arrays makes them invaluable for organizing and managing large sets of information efficiently. Additionally, PHP offers various methods to add or remove elements from an array dynamically, allowing developers to modify the contents based on their specific requirements.
To create a numeric array in PHP, one simply needs to assign values to the desired indexes within square brackets. For instance: $numbers = [1, 2, 3].
Associative Arrays: Developers can store key-value pairs using the core PHP programming technique known as associative arrays. Associative arrays utilise string or integer keys as opposed to standard arrays, which use numeric indices. Due to their adaptability, they are exceptionally strong and versatile for handling data organization and manipulation in PHP applications.
In PHP, associative arrays are created using the array() function and assigning values to specific keys. For example, we can create an associative array called $person with keys such as “name”, “age”, and “occupation”. We can then assign values like “John Doe”, 25, and “Developer” respectively to these keys. Accessing the values is as simple as referencing the desired key within square brackets, such as $person[“name”].
Multidimensional Arrays:
Multidimensional arrays, which can store sophisticated data structures, can have more dimensions than normal arrays, which can only have one. Because of this, they are very helpful when handling vast amounts of connected data.
Nesting one or more arrays inside another array results in multidimensional arrays. A separate dimension of the overall structure is represented by each layered array. For instance, a grid of values could be represented as a two-dimensional array with rows and columns acting as the various dimensions. Developers can retrieve or change specific values from the multidimensional array by accessing certain elements using their corresponding indices in each dimension.