---
title: "Array in PHP"  
description: "An array is a data structure that store multiple values in single variable. In PHP to create an array, array() function is used.    $mobile = array(\"S"  
author: "zack mathews"  
published: 2016-06-06  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11140/array-in-php  
category: "php"  
tags: ["php", "array", "array list"]  
reading_time: 3 minutes  

---

# Array in PHP

An array is a **[data structure](https://www.mindstick.com/blog/11221/simple-way-to-learn-dynamic-data-structure-in-c-language)** that store [multiple values](https://www.mindstick.com/forum/399/how-to-insert-multiple-values-selected-in-checkbox-in-database) in single [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation). In PHP to create an array, array() [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) is used.\

```
<?php
$mobile = array("Samsung", "Nokia", "Micromax"); //$mobile is a single variable that contain many similar types of values.
echo "I like " . $mobile[0] . ", " . $mobile[1] . " and " . $mobile[2] . ".";
?>
```

Output:

I like Samsung, Nokia and Micromax.

## Basically array are of three types:

**Indexed array****:**

These array can store numbers, object and strings but their index is represented by numbers. [Default value](https://www.mindstick.com/forum/2035/default-value-when-using-singleordefault) of any index is 0.

```
<?php
$mobile = array("Samsung", "Nokia", "Micromax");
foreach($mobile as $k=>$v)
echo("$k:$v.</br>");
?>
```

Output:

0:Samsung.//default value is 0

1:Nokia.

2:Micromax.

##### Associative arrays:

The [associative array](https://www.mindstick.com/forum/606/length-of-javascript-object-ie-associative-array) are similar to index array in terms of [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) but they are different in terms of **key/index.**

**Note:**These types of array are not [kept inside](https://answers.mindstick.com/qa/116165/explain-controllers-in-angularjs-what-should-not-be-kept-inside-a-controller) double quotes else it will not return any value.

```
<?php
$salary = array("ragini"=>"35000", "Shikha"=>"40000", "Megha"=>"45000");
echo "Shikha salary is " . $salary['Shikha'];
?>
```

Output:

Shikha salary is 40000

Multi-dimensional array

A multi-dimensional array consist one or more arrays. Below is an example in which 3 [students](https://www.mindstick.com/articles/13083/benefits-of-students-who-can-get-online-assignment) marks of three subject are stored in 2-dimensional array.

```
<?php
         $record = array(            "Rakesh" => array (               "Hindi" => 52,               "English" => 34,                 "History" => 33
            ),
            "salman" => array (               "Hindi" => 39,               "English" => 35,               "History"=> 34            ),
            "Shikha" => array (
               "Hindi" => 37,               "English" => 29,               "History" => 31
            )         );
         /* Accessing multi-dimensional array values */
         echo "Marks for Rakesh in History : " ;         echo $record['Rakesh']['History'] . "<br />";         echo "Marks for salman in English : ";         echo $record['salman']['English'] . "<br />";         echo "Marks for Shikha in Hindi : " ;         echo $record['Shikha']['Hindi'] . "<br />";
      ?>
```

##### Output

Marks for Rakesh in History : 33Marks for salman in English : 35Marks for Shikha in Hindi : 37

---

Original Source: https://www.mindstick.com/blog/11140/array-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
