---
title: "Array in PHP"  
description: "Array is collection of same data types. In PHP, an array stores multiple values in one single variable. I.e. an array is a special variable, which can"  
author: "Anonymous User"  
published: 2011-09-08  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/721/array-in-php  
category: "php"  
tags: ["php"]  
reading_time: 4 minutes  

---

# Array in PHP

Array is collection of same data types. In PHP, an array stores multiple values in one single variable. I.e. an array is a special variable, which can store multiple values in one single variable.\

In PHP, there are three types of array.

1. Numeric Array
2. Associative Array
3. Multidimensional Array

##### Numeric Array:

A numeric array stores each array element with a numeric index. i.e. When we create numeric array, index automatically or manually assigned for each value of array at creation time.

There are two methods to create numeric array.

##### 1. Index automatically assigned( index starts at 0)

Syntax : $array_name = array (element1, element 2,……);

##### 2. Index manually assigned.

Syntax: $array_name[0] = element1;

$array_name[1] = element2;

..........................

..........................

$array_name[N] = elementN ;

Let’s we have an example, how to implement numeric array in php.

##### Example:

```
<html><head>        <title> Numeric Array</title>        </head><body>        <?php              //  Declare array variable with automatically index              $array_auto =array (1,2,3,4,5,6,7,8,9);                //  Declare array varraible with manual index              $array_manual [0]  = "Arun";              $array_manual [1]  = "Kumar";              $array_manual [2]  = "Singh";         // Print array with autmatically index        print_r  ($array_auto) ;         // print array with manual index        print_r ($array_manual) ;               ?></body> </html>
```

##### Output:

![Array in PHP](https://www.mindstick.com/mindstickarticle/299c088c-605e-42ed-b115-ea169dbf9d92/images/f8b7e793-a762-4995-a3f7-1f3fe0715368.png)

**Note:** ‘print_r’ is the function to print all values of array.

##### Associative Array:

An associative array, each ID key is associated with a value. When storing data about specific named values, a numerical array is not always the best way to do it. With associative arrays we can use the values as keys and assign values to them.

##### Syntax: Create associative array

$ass_array = array ( "index_Id1" => value , "index_id1" => value, .........);

##### Example: Let’s we have an example, how to implement Associative array in PHP.

```
<html><head>        <title> Switch Statement</title>        </head><body>        <?php              //  Declare associative array variable              $ass_array = array ("Arun" => 22, "Vineet" => 24, "Pandey" => 25);                print_r ($ass_array);                      ?></body> </html>
```

##### Output:

![Array in PHP](https://www.mindstick.com/mindstickarticle/299c088c-605e-42ed-b115-ea169dbf9d92/images/374fa789-667b-475d-acbd-01cb9a1981b4.png)

##### Multidimensional Array:

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

##### Syntax:

```
 $multi_array = array (    ele1 => array1    (      subele1,      subele2,      .......      .......      subeleN    ),    ele2 => array2    (      subele1,      subele2,      .......      ........      ),    ........    ........    eleN = arrayN ()    ) ;
```

Let’s we have an example, how to implement multidimensional array.

##### Example:

```
<html>        <head>   <title> Multidimensional Arrray Test </title>        </head><body>        <?php              //  Declare multidimensional array              $multi_array = array(                            "Author" => array (                                  "Twash",                                  "Brooker",                                  "Hinggbotton"                                  ) ,                            "Subject" => array (                                     "Math",                                     "Physics",                                     "Chemistry"                                      ) ,                            "School" => array (                                    "Bal vikas",                                    "Jaipuria",                                    "Anandram"                                  )                           );              // Print multidimensional array with print_r function                print_r ($multi_array)     ;                  ?>  </body></html>
```

##### Output:

![Array in PHP](https://www.mindstick.com/mindstickarticle/299c088c-605e-42ed-b115-ea169dbf9d92/images/c896d2b8-f9c7-4094-80a4-1716397a26c3.png)

Example: Let’s we have an example, how to implement one-dimensional, two-dimensional and multi-dimensional array in php.

```
<html>        <head>   <title> Array Test  </title>        </head><body><?php//  Declare   one- dimensional arrayecho "<h3>"."This is one- dimensional array"."</h3>"."</br>";$subject = array("Math","Physics","Chemistry","English","Hindi","Geography","Civics","History","Economics","Political Science");              // print one dimensional element using foreach loopforeach ($subject as $value){  echo ($value."</br>");}        // Declare two-dimensional array        $matrix  = array                      ( array (1,2,3),                       array (4,5,6),                       array (7,8,9)                     );    echo "<h3>"."This is two dimensional array"."</h3>"."</br>";// Print two dimensional array element        for($row = 0 ; $row <3 ; $row++)         {           for($col = 0 ;$col <3 ;  $col++)              {                echo $matrix[$row][$col]."\t"."\t";               }               echo "</br>";       }// Declare multi-dimensional array$organization = array (array (array ("Tech","Ankur","kumar"),                             array  ("Sales","Arvind","javed"),                             array  ("HR", "Bhabha","Phoole")                             ),                       array (array  ("Tech","Govind","Reimya"),                             array  ("Sales","Guru","Chaurasia"),                             array  ("HR", "Bhabha","Phoole")                             )                      );echo "<h3>"."This is multidimensional Array"."</h3>"."</br>";                     for($layer = 0 ; $layer <2 ; $layer++) {  for($row =0; $row<3;$row++)   {    for($col =0;$col<3 ;$col++)       {         echo $organization[$layer][$row][$col]."\t";       }  echo "</br>" ; } echo "</br>"; } ?>  </body></html>
```

##### Output:

![Array in PHP](https://www.mindstick.com/mindstickarticle/299c088c-605e-42ed-b115-ea169dbf9d92/images/e2b4c00f-cf40-4f68-8010-874589ae1c03.png)

---

Original Source: https://www.mindstick.com/articles/721/array-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
