PHP array is a linear data structure that can store more than one values under a single variable.
Types of Arrays
There are three different kind of arrays and each array value is accessed using an ID, which is called array index.
- Numeric/Indexed Array − An array with a numeric index. Values are stored and accessed in linear fashion.
- Associative array − An array with strings as an 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.
An indexed or numeric array stores each array element with a numeric index.
<?php $fruit = array("Mango", "Papaya", "Orange"); echo "I would like to eat " . $fruit . ", " . $fruit . " and " . $fruit . "."; ?>
Run : http://ideone.com/OuXU3A
I would like to eat Mango, Papaya and Orange.
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.
<?php $age = array("Symntha"=>"15", "Clan"=>"17", "Smith"=>"23"); echo "Symntha is " . $age['Symntha'] . " years old."; ?>
Run : http://ideone.com/BFRnAf
Symntha is 15 years old.
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.
<?php $cars = array ( array("Elentra",22,18), array("Audi",15,13), ); echo $cars.": In stock: ".$cars.", sold: ".$cars.".\n"; echo "<br>"; echo $cars.": In stock: ".$cars.", sold: ".$cars.".\n"; ?>
Run : http://ideone.com/0o2e9P
Elentra: In stock: 22, sold: 18.
Audi: In stock: 15, sold: 13.
|Exercises & Assignments|
|Remove specific element by value from an array in PHP?|
|PHP Array to String Conversion (favourite colours chosen by user)|
|How to check if an array is a subset of another in PHP?|
|Interview Questions & Answers|
|No Content Found.|