PHP Arrays

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:

$fruit1 = “apple”;
$fruit2 = “kiwi”;
$fruit3 = “papaya”;

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.

Indexed array

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:


// Define an indexed array
$fruits= array("Apple", "Orange", "Papaya");


Array ( [0] => Apple [1] => Orange [2] => Papaya)

b. Indexes are Assigned Manually Way:


$fruits[0] = "Apple"; 
$fruits[1] = "Orange"; 
$fruits[2] = "Papaya"; 


Array ( [0] => Apple [1] => Orange [2] => Papaya)

Associative array

Pin It on Pinterest

Share This