Table of Contents

Conclusion

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. 

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

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>

Become a Certified UI UX Expert in Just 5 Months!

UMass Amherst UI UX BootcampExplore Program
Become a Certified UI UX Expert in Just 5 Months!

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>

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

  • 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.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 30 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449