PHP Arrays is the group of similar data elements having the contiguous memory location.In array, first element represents the lowest address and last element represents the highest address and element of array is uniquely identified via index number that starts with 0. PHP Arrays is of several types such as Associative Array, Indexed Array, Multi-dimensional Array. For example, if you want to make the cart for shopping sites in PHP then you have to use Associative Array in PHP which accepts the values using key and value.
At eitworld.com you can learn PHP Arrays in simple way with lots of explained examples.
- Arrays are the variables which are used to store multiple values.
Need of array
- What is the need of array while you can create as many individual variables as you want?
- Suppose if you have 500 values to store for example you want to save a record of students in a school. It will be irritating and almost impossible to create 500 different variables for each detail of the students. Here comes the need of array. You can create a single array and save all the 500 values in it. There are very less variables to handle when we use array.
- If you want to search for a particular value from many variables, this is not possible while individual variables are there. But in array, you can easily loop through the array to access all the values in it, find your desired value and manipulate it according to your need.
Syntax of Array
- In php array() function is used to create an array.
This is the basic form of creating an array. $arr is becomes an array which will contained the values that are written inside array() function. count($arr) returns the length of the array which can be used to loop through the array.
Types of array
- There are three types of array in PHP:
- Indexed Array- These arrays have numeric index.
- Associative Array- These arrays have named index.
- Multidimensional Array- These arrays themselves have more than one array.
- Indexed array are created in following way-
- In above array, the index will be started from 0. The index for value1 will be 0, for value2 will be 1, for value3 will be 2 and so on.
- You may also assign values individually using index-
- Use a for loop to go through entire array and use its counter as the index of array to access the values.
- Above example creates an array variable $bowlers. The length of the array is obtained in $length. Now we are accessing the values using for loop. The for loop is run till the length of array and the values are accessed one by one in each loop.
- Associative arrays can be created in following way-
- Here both indexes and values must be specified in array() function.
- You may also assign values individually using index.
- Looping in associative arrays is done in following manner-
foreach($arr as $index=>$values)
- Now $index will be representing the indexes of the array and $values will represent the values at respective indexes. See the example-
foreach($bowlers as $name=>$wicket)
echo "For Index= ".$name.", Value= ".$wicket;
- An array which contains more than one array is called Multidimensional Array.
- Two dimensional array, Three Dimensional array, Four Dimensional array etc are the types of Multidimensional Array. In this tutorial we will learn the two dimensional form of multidimensional array because arrays more than two dimensions are typical to manage.
Two Dimensional Array
- An array of arrays is known as Two Dimensional Array.
- Here is the basic syntax of two dimensional array which contains name and age of two persons:
$person = array