PHP Arrays


In simple words an array is a special variable, which can hold more than one value at a time. An array is a collection of values that are belonging to a same data type. Each value is an element of the array and has an associated index number. We can refer to a particular element in the array by using the name of the array and the index number. The index number starts at zero. The list of array values that are separated by comma and must have to belong between opening and closing bracket.

In PHP, the array() function is used to create an array :
 array();

In PHP there are three types of array  :

1. Indexed arrays
2. Associative arrays
3. Multidimensional arrays

1. Index arrays :

Index array is an array with a numeric key. The array index start from zero. There are two ways to create indexed arrays. The first one is by using the array() function, with which we can automatically assigned the values on it. For example :
 $array_name = array(value1, value2, value3, ...., valueN);
Example :
<?php
  $WebDev = array("HTML", "CSS", "JavaScript", "PHP", "MySql");
  echo "The Web Devlopment Languages are<br/><br/>";
  echo "Front End : " . $WebDev[0] . ", " . $WebDev[1] . ", " . $WebDev[2] . "<br/>";
  echo "Back End : " . $WebDev[3] . ", " . $WebDev[4];
?>
Output :

The Web Devlopment Languages are

Front End : HTML, CSS, JavaScript
Back End : PHP, MySql

Or we can also assign the values manually like :
 $array_nam[0] = value1;
 $array_nam[1] = value2;
 $array_nam[2] = value3;
 $array_nam[N-1] = valueN;
Example :
<?php
  $WebDev[0] = "HTML";
  $WebDev[1] = "CSS";
  $WebDev[2] = "JavaScript";
  $WebDev[3] = "PHP";
  $WebDev[4] = "MySql";

  echo "The Web Devlopment Languages are<br/><br/>";
  echo "Front End : " . $WebDev[0] . ", " . $WebDev[1] . ", " . $WebDev[2] . "<br/>";
  echo "Back End : " . $WebDev[3] . ", " . $WebDev[4];
?>
Output :

The Web Devlopment Languages are

Front End : HTML, CSS, JavaScript
Back End : PHP, MySql


2. Associative arrays

Associative arrays are very similar to numeric arrays in terms of functionality but they are different in terms of their index. In associative array, to access the array values, the user defined keys are used instead of index numbers. Associative array will have their index as string so that user can establish a strong association between key and values. For example :
 $array_name = array(key1 => value1, key2 => value2, key2 => value3, ...., keyN => valueN);
Example :
<?php
  
  $age = array("John"=> 25, "Sam" => 23, "Marry" => 30, "Luci" => 29);

  echo "John's Age : " . $age['John'] . "<br/>";
  echo "Sam's Age : " . $age['Sam'] . "<br/>";
  echo "Marry's Age : " . $age['Marry'] . "<br/>";
  echo "Luci's Age : " . $age['Luci'] . "<br/><br/>";


  $salary["John"] = 20000;
  $salary["Sam"] = 18000;
  $salary["Marry"] = 40000;
  $salary["Luci"] = 36000;
  
  echo "John's salary : " . $salary['John'] . "<br/>";
  echo "Sam's salary : " . $salary['Sam'] . "<br/>";
  echo "Marry's salary : " . $salary['Marry'] . "<br/>";
  echo "Luci's salary : " . $salary['Luci'] . "<br/><br/>";

?>
Output :

John's Age : 25
Sam's Age : 23
Marry's Age : 30
Luci's Age : 29

John's salary : 20000
Sam's salary : 18000
Marry's salary : 40000
Luci's salary : 36000


3. Multidimensional arrays

The multidimensional array is an array in which each element can also be an array and each element in the sub-array can be an array or further contain array within itself and so on. Values in the multi-dimensional array are accessed using multiple index. For example :
 $array_name = array(
   array(
     key1 => value1,
     key2 => value2
     keyN => valueN
   ),
   array(
     key1 => value1,
     key2 => value2
     keyN => valueN
   ),
   array(
     key1 => value1,
     key2 => value2
     keyN => valueN
   )
 );
Example :
<?php

  $employees = array(
    array(
      "Name" => "John snow",
      "Age" => "35",
      "Salary" => "20000"
    ),
    array(
      "Name" => "Luci hunt",
      "Age" => "24",
      "Salary" => "15000"
    ),
    array(
      "Name" => "James bond",
      "Age" => "55",
      "Salary" => "75000"
    )
  );

  echo "Employee details : <br/><br/>";

  echo "Name : " . $employees[0]["Name"] . "<br/>";
  echo "Age : " . $employees[0]["Age"] . "<br/>";
  echo "Salary : " . $employees[0]["Salary"] . "<br/><br/>";

  echo "Name : " . $employees[1]["Name"] . "<br/>";
  echo "Age : " . $employees[1]["Age"] . "<br/>";
  echo "Salary : " . $employees[1]["Salary"] . "<br/><br/>";

  echo "Name : " . $employees[2]["Name"] . "<br/>";
  echo "Age : " . $employees[2]["Age"] . "<br/>";
  echo "Salary : " . $employees[2]["Salary"] . "<br/><br/>";
?>
Output :

Employee details :

Name : John snow
Age : 35
Salary : 20000

Name : Luci hunt
Age : 24
Salary : 15000

Name : James bond
Age : 55
Salary : 75000


Next Topic :