PHP Array


What is PHP array

PHP array is a form of data structure that helps one to store several elements of a particular category of data under one single variable, thereby saving us the work to construct a separate variable for increasing data. The arrays are useful in generating a list of related types of elements that can be accessed using their index or link. Suppose we have five names to store and print them out appropriately. This can be achieved simply by using five separate variables for the series. So if the amount increases to a hundred instead of five, then making too many separate variables will be incredibly challenging for the consumer or creator. Here array comes into play and allows us to store each item within a single variable and also facilitates quick access using an index or key. In PHP, an array is generated using a function array().

PHP essentially consists of three types of arrays:

Indexed or Numeric Arrays: An collection of statistical indexes. Values are processed in linear fashions and retrieved.
Associative Arrays: An sequence of strings as an index. This stores values of elements not in a specific linear index sequence, but in combination with main values.
Multidimensional Arrays: Using several indices an index that includes one or more objects and values are obtained.

PHP array

Indexed or Numeric Arrays

An indexed or numeric array stores a numeric index for any array element. Such kinds of arrays may be used to store some kind of components, but an index is a number only. The index must start at 0, by default. Numeric arrays use the number as the keys for entry. An access key is a pointer to an array variable in memory space. The access key is used if we want an array variable to be read or allocated a new value.


Associative Arrays

In terms of features, the associative arrays are very similar to numeric arrays but vary in terms of their index. The associative array should have its index as a string such that a clear correlation between key and values can be formed.

A numerically sorted list would not be the right option to hold the workers’ wages in an database. Alternatively we should use the names of the workers as the keys in our associative sequence, and their respective salaries will be the interest.

array(“PeterAge”=>19, “JordanAge”=>30, “WilliamAge”=>26);

Multidimensional Arrays

These are arrays that contain additional nested arrays. The benefit of multidimensional arrays is that they help us group together similar data. Multi-dimensional arrays are those arrays that store a separate array at each index, rather than a single part. To put it another way, we may describe multi-dimensional arrays like arrays. Every element in this array can be an array as the name suggests, and they can also hold other sub-arrays within. Multidimensional arrays or sub-arrays can be reached utilizing different dimensions.

“name” => “Petter Parker”,
“mobile” => “123****789”,
“name” => “Jordan Smith”,
“mobile” => “987****321”,
“name” => “William Smith”,
“mobile” => “632****123”,