PHP Array

Array is a Secondary Data Type. Secondary Data Type is a type of data that is basically made of Primary Data Types but their capability is much higher than Primary Data Types.

Array is one of the simplest data structures. In Computer we do not store any data in a systematic way, we can not access, manipulate and manage that data properly.

PHP has a collection of arrays variables that store many values ​​of the same or different data type at the same time.

<?php
$fruits = array("Mango", "Banana", "Apple");
echo "I like " . $fruits[0] . ", " . $fruits[1] . " and " . $fruits[2] . ".";
?>

It is easy to systematize and store data quickly and efficiently using arrays, it is one of the more useful data types available for any programming language.

In PHP, all the array is helpful, but you can use a numerical index to reach them. An array with a numerical index is usually called an indexed array, whereas one is given a positive name, which A associate array is called.

Types of PHP Arrays

  • Indexed Arrays
  • Associative Arrays
  • Multidimensional Arrays

PHP Indexed Array

Multiple elements are stored in the indexed array.

Index 0 starts with index, and the number of elements that are in is equal to -1. The element of each array is seperate with the comma.

Syntax

$arr = array(value 1, value 2, value 3, value 4, ........, value n);
       or
$arr[0] = value 1;
$arr[1] = value 2;
$arr[2] = value 3;

Example

<?php
$studentName = array("Ram", "Shyam", "Mohan", "Sohan");

echo $studentName[0] . ", " . $studentName[1] . ", " . $studentName[2] . ", " . $studentName[3];
?>

Output

Ram, Shyam, Mohan, Sohan

PHP Associative Arrays

Associative Array; Indexed Array is similar, but in the associative array, the programmer defines the index.

To define index value is used with => this sign. The element of each array is seperate with the comma.

Syntax

$arr = array(key 1 => value 1, key 2 => value 2, key 3 => value 3, ......., key n => value n);
       or
$arr[key 1] = value 1;
$arr[key 2] = value 2;
$arr[key 3] = value 3;

Example 1

<?php
$rollNumber = array("Rohan"=>"1", "Mohan"=>"2", "Sohan"=>"3");
echo "Rohan Roll Number is: " . $rollNumber["Rohan"];
?>

Output

Rohan Roll Number is: 1

Example 2

<?php
$rollNumber = array("Rohan"=>"1", "Mohan"=>"2", "Sohan"=>"3");

foreach($rollNumber as $a_key => $a_value) {
    echo $a_key . " Roll Number is:" . $a_value;
    echo "<br>";
}
?>

Output

Rohan Roll Number is: 1
Mohan Roll Number is: 2
Sohan Roll Number is: 3

PHP Multidimensional Array

Multidimensional Array in PHP is also called Nested Array. The way we can store a Scalar type value in the form of a value in an Array, in the same way when we store any other Array as Value, an Array is a type of There is a multidimensional array.

MultiDimensional Array consists of two or more arrays.

Syntax

$arr = array(
             array(value 1, value 2, value 3, ........, value n),
             array(value 1, value 2, value 3, ........, value n),
             array(value 1, value 2, value 3, ........, value n)
);

Example for MultiDimensional Array using for Loop

<?php
$arr = array(
            array(5,5,9,2,9),
            array(3,6,2,1,7),
            array(6,0,8,1,9),
            array(8,5,0,9,4)
);

for ($row = 0; $row < 4; $row++) {
  for ($col = 0; $col < 5; $col++) {
    echo $arr[$row][$col]." ";
  }
  echo "<br>";
}
?>

Output

5 5 9 2 9
3 6 2 1 7
6 0 8 1 9
8 5 0 9 4

Example for MultiDimensional Array using foreach Loop

<?php
$arr = array(
            array(5,5,9,2,9),
            array(3,6,2,1,7),
            array(6,0,8,1,9),
            array(8,5,0,9,4)
);

foreach ($arr as $row){
         foreach($row as $col){
          echo $col . " ";
         }
     echo "<br>";
}
?>

Output

5 5 9 2 9 
3 6 2 1 7
6 0 8 1 9
8 5 0 9 4