An array is a special type of variable in PHP that can hold a list of items, or we can say it's a special type of variable that can hold one or more values at a time.

Suppose we are having a list of food names, and if we store the food names in the separate variables as shown below:

$food1 = "Pizza";

$food2 = "Burger";

$food3 = "French Fry";

Here, it’s easy to find a specific food out of three food names. 

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

Now suppose you need to loop through the food names and find a specific food? And what if there are 100 food names instead of three food names?

In such scenarios, we use arrays as we can store many values under one variable name and can get access to our required item using an index number.

To create an array in PHP, we generally use the array() function.

array();

Now, let us see an example of creating an array in PHP using the array() function.

<html>

<body>

    <?php

         /* First method to create array by using automatic indexing method. */

         $numbers = array( 1, 2, 3, 4, 5);

         /*here we have used foreach to iterate through the elements of numbers array*/

         foreach( $numbers as $value ) {

            echo "Value is $value <br />";

         }

         /* Second method to create array using manual indexing method. */

         $numbers[0] = "one";

         $numbers[1] = "two";

         $numbers[2] = "three";

         $numbers[3] = "four";

         $numbers[4] = "five";

         /*here we have used foreach to iterate through the elements of numbers array*/

         foreach( $numbers as $value ) {

            echo "Value is $value <br />";

         }

      ?>

</body>

</html>

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

In PHP, there are three types of arrays.

  1. Indexed Array: These are the arrays with a numeric index.
  2. Associative Array: These are the arrays with named keys.
  3. Multidimensional Array: These arrays contain one or more arrays within themselves.

  • Indexed / Numeric Array

PHP indexed array can store numbers, strings or any object. 

There are two methods of creating the indexed array:

Method 1: Here, the index can be assigned automatically, as shown below. Indexing by default starts from 0 in this method.

$food = array("Pizza", "Burger", "French Fry");

Method 2: We can manually assign the index as shown below. 

$food[0] = "Pizza";

$food[1] = "Burger";

$food[2] = "French Fry";

Example:

Here, we will create an indexed array named food which will store three food names as values.  

Then, we will find an output statement containing all the food array values:

<!DOCTYPE html>

<html>

<body>

    <?php

$food = array("Pizza", "Burger", "French Fry");

echo "The food I like the most are " . $food[0] . ", " . $food[1] . " and " . $food[2] . ".";

?>

</body>

</html>

Now let us see the code for looping through the indexed food array: 

<!DOCTYPE html>

<html>

<body>

<?php

$food = array("Pizza", "Burger", "French Fry");

$arrlength = count($food);

for($i = 0; $i < $arrlength; $i++) {

  echo $food[$i];

  echo "<br>";

}

?>

</body>

</html>

The above code will loop through our food array and return the food names in different lines.

  • Associative Array 

We assign some named keys to these array types, and the Associative Arrays then use these named keys.

There are two methods to create these types of arrays:

Method 1:

$foodPrice = array("Pizza"=>"150", "Burger"=>"120", "French Fry"=>"99");

Method 2:

$foodPrice['Pizza'] = "150";

$foodPrice['Burger'] = "120";

$foodPrice['French Fry'] = "99";

In scripts, the named keys can be used in the following way.

<!DOCTYPE html>

<html>

<body>

<?php

$foodPrice = array("Pizza"=>"150", "Burger"=>"120", "French Fry"=>"99");

echo "Pizza costs " . $foodPrice['Pizza'] . " rupees only";

?>

</body>

</html>

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

  • Multidimensional Array 

Sometimes, we need to store more than one key for the values. For such a purpose, we use a multidimensional array. 

PHP uses multidimensional arrays that can be one, two, three, four, or more levels deep.

Now let us see an example of a two-dimensional array in PHP.

Example:

A two-dimensional array is an array of arrays.

Below is a table of data. Let us see how we can store this table in a two-dimensional array in PHP.

Name

Stock

Sold

Pizza

22

18

Burger

15

13

French Fry

5

2

Two dimensional array implementation in PHP: 

$food = array (

  array("Pizza",22,18),

  array("Burger",15,13),

  array("French Fry",5,2)

);

In the above code, we have implemented a two-dimensional $food array containing three arrays with two indices, row and column. 

We must point to the two indices of row and column to get access to the $food array elements.

Let us see how we can get access to the elements.

<!DOCTYPE html>

<html>

<body>

<?php

$food = array (

  array("Pizza",22,18),

  array("Burger",15,13),

  array("French Fry",5,2)

);

echo $food[0][0].": In stock: ".$food[0][1].", sold: ".$food[0][2].".<br>";

echo $food[1][0].": In stock: ".$food[1][1].", sold: ".$food[1][2].".<br>";

echo $food[2][0].": In stock: ".$food[2][1].", sold: ".$food[2][2].".<br>";

?>

</body>

</html>

To get the $food array elements, we can also use a for loop inside another for loop. 

<!DOCTYPE html>

<html>

<body>

<?php

$food = array (

  array("Pizza",22,18),

  array("Burger",15,13),

  array("French Fry",5,2)

);

for ($row = 0; $row < 3; $row++) {

    echo "<p><b>Row number $row</b></p>";

    echo "<ul>";

    for ($col = 0; $col < 3; $col++) {

      echo "<li>".$food[$row][$col]."</li>";

    }

    echo "</ul>";

  }

?>

</body>

</html>

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

So, arrays are essential for storing multiple elements under the same variable name in PHP. We can also access the elements randomly by using the index number (zero-based). We can also represent the matrices by using multidimensional arrays. These arrays allocate contiguous memory locations for the array elements. Using arrays, we can also implement complex data structures like stacks, queues, etc. 

Arrays are, therefore, a crucial concept for programming.

To know more about Arrays in PHP, you can enroll in the Post-Graduate Program in Full-Stack Web Development offered by Simplilearn in collaboration with Caltech CTME. The course details everything you need to become a full-stack technologist and accelerate your career as a software developer.

Simplilearn also offers free online skill-up courses in several domains, from data science and business analytics to software development, AI, and machine learning. You can take up any of these free courses to upgrade your skills and advance your career.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.