PHP Arrays
PHP array is an ordered map [contains value on the basis of key]. It is used to hold multiple values of similar type in a single variable.
Advantage of PHP Array
Less Code: We don't need to define multiple variables.
Easy to traverse: By the help of single loop, we can traverse all the elements of an array.
Sorting: We can sort the elements of array.
PHP Array Types
There are 3 types of array in PHP.
- Indexed Array
- Associative Array
- Multidimensional Array
PHP Indexed Array
PHP index is represented by number which starts from 0. We can store number, string and object in the PHP array. All PHP array elements are assigned to an index number by default.
There are two ways to define indexed array:
1st way:
2nd way:
Example
File: array1.phpOutput:
Season are: summer, winter, spring and autumn
File: array2.phpOutput:
Season are: summer, winter, spring and autumn
Click me for more details...PHP Associative Array
We can associate name with each array elements in PHP using => symbol.
There are two ways to define associative array:
1st way:
2nd way:
Example
File: arrayassociative1.phpOutput:
Sonoo salary: 350000 John salary: 450000 Kartik salary: 200000File: arrayassociative2.php
Output:
Sonoo salary: 350000 John salary: 450000 Kartik salary: 200000Click me for more details...
PHP Multidimensional Array
Click me for more details...
An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.
There are three different kind of arrays and each array value is accessed using an ID c which is called array index.
Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion.
Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices
NOTE − Built-in array functions is given in function reference PHP Array Functions
Numeric Array
These arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero.
Example
Following is the example showing how to create and access numeric arrays.
Here we have used array[] function to create array. This function is explained in function reference.
This will produce the following result −
Value is 1 Value is 2 Value is 3 Value is 4 Value is 5 Value is one Value is two Value is three Value is four Value is five
Associative Arrays
The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.
To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.
NOTE − Don't keep associative array inside double quote while printing otherwise it would not return any value.
Example
This will produce the following result −
Salary of mohammad is 2000 Salary of qadir is 1000 Salary of zara is 500 Salary of mohammad is high Salary of qadir is medium Salary of zara is low
Multidimensional Arrays
A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.
Example
In this example we create a two dimensional array to store marks of three students in three subjects −
This example is an associative array, you can create numeric array in the same fashion.
This will produce the following result −
Marks for mohammad in physics : 35 Marks for qadir in maths : 32 Marks for zara in chemistry : 39