An array is a data structure that holds multiple values in one single variable at a time.
Let’s see you have a multiple items (a list of fruit names, for example), storing the fruit in single variables could look like this:
Now if you want to loop through the fruits and find a specific one? And what if you had lots of fruit ?
The solution is to create an array for this problem!. Let’s starts with how to create an array?.
In PHP, the array() function is used to create an array:
Types Of Array
There are three types of array in PHP which is given below and describing array one by one:
- Indexed array — An array with a numeric element.
- Associative array — An array where each element has its own specific value.
- Multidimensional array — An array containing one or more arrays within itself.
An indexed or numeric array holds each array value with a numeric index. There are two ways of creating an indexed array.
a. Simple Way: The index can be assigned automatically (index always starts at 0) .For an example see given below:
<?php // Define an indexed array $fruits= array("Apple", "Orange", "Papaya"); ?>
b. Indexes are Assigned Manually Way:
<?php $fruits = "Apple"; $fruits = "Orange"; $fruits = "Papaya"; ?>