Working on Array
×
Home Courses Community Support
Working on Array

You have learned about Array in PHP data types. To create an array we used the function array().

We have three type of array in PHP-

  • Indexed array
  • Associative array
  • Multidimensional array


Indexed Array

Indexed array is an array with numeric index, that's why it is called Numeric array too. In this array value is stored/retrived in linear fashion.

Below is the example -

<?php 
//We have learned in Data Types
$peoples = array("Anuj","Ankur","Anil");
echo $peoples[0]."<br>";

// New method to learn
$string[0] = "Anuj";
$string[1] = "Ankur";
$string[2] = "Anil";
echo $string[2];
?>

Output:

Anuj

Anil


Associative Array

Associative arrays are very similar to Indexed array. Both have only one difference that you can assign your key and value in associative array.

See below example -

<?php
$salary = array('Ram Kumar'=>"50,000", 'Shyam Singh'=>"70,000", 'Mohamad Abdul'=>"7,000");
echo $salary['Ram Kumar'];
?>

Output:

50,000


Multidimensional Array

A multidimensional array is an array where each and every element in an array can be a array. And each and every element in a subarray can also be an array and so on... So we can say that an array havin more then one level in deep can be called Multidimensional Array.

See below example for two dimensional array which is a HR record file, to hire new employees-

Name Expected Salary Experience
Ram Kumar 50,000 5 Years
Shyam Singh 70,000 7 Years
Mohamad Abdul 7,000 Fresher


Code for this example is as below -

<?php
$employee = array( array('Ram Kumar','50,000','5 Years'), array('Shyam Singh','70,000','7'), array('Mohamad Abdul','7,000','Fresher'));
echo $employee[0][0]. " expecting salary INR ".$employee[0][1].'.' . " He has working experience of " .$employee[0][2].'.';
?>

Output:

Ram Kumar expecting salary INR 50,000. He has working experience of 5 Years.

 

Array employee has three sub arrays. Sub arrays have the record of employees to be hired. In above code we print the record of first sub array (Index id is '0'). So we print all values by using it's index id and index id of sub array data.

 

← Preview

Dynamic Function Call

Next →

Array Sorting

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App