Array in PHP

Types of Array There are three types of arrays in PHP, which are as follows:

  •  Numeric Index Array
  • Associative Array
  • Multidimensional Array
Numeric Index Array A numeric index array stores each array element with a numeric index i.e. subscript. In a numeric indexed array, the keys are numeric and starts with subscript of 0, the next is 1, and so on. Numeric indexed arrays use integer/numbers as their index number to identify each item of the array. Syntax of Numeric Index Array There are two ways to define the numeric index array in PHP. Which are as follows: 1. $arrayname =array (“value1″,”value2″,”value3”); 2. $arrayname[0]=”value1″; $arrayname[1]=”value2″; $arrayname[2]=”value3″; Example1
<?php
$empname = array ("Anuj kumar","Sanjeev Kumar","Rahul Chaubey");
echo "This is the First Example of Numeric Index Array".'<br>'.'<br>';
echo $empname[0].'<br>';
echo $empname[1].'<br>';
echo $empname[2];
?>
Output numerciindexarray Example2
<?php
$empname[0]="VAnuj kumar";
$empname[1]="Sanjeev Kumar";
$empname[2]="rahul Chaubey";
echo "This is the Second Example of Numeric Index Array ".'<br>' ;
echo '<br>' .$empname[0]. '<br>' .$empname[1]. '<br>' .$empname[2] ;
?>
Output

numericindexarray2

Associative Array An Associative array also is a type of array by which you can assign an arbitrary key to every value. In an associative array, each key is associated with a value. With associative arrays we can use the values as keys and assign values to them. In an associative array, the keys are not necessarily numeric, and even when they are numeric, not necessarily in any order. Syntax of Associative Array There are two ways for defining the associative array in PHP. Which are as follows: 1. $arrayname = array (“value”=>20, “value2″<=30, “value3″=>40); 2. $arrayname[‘value1’] = “20”; $arrayname[‘value2’] = “30”; $arrayname[‘value3’] = “40”; Example1
<?php
$studentpercent ['Anuj'] = "76";
$studentpercent ['Sanjeev'] = "68";
$studentpercent ['Rahul'] = "90";
echo "This is First Example of Associative Array" .'<br>'.'<br>';
echo "Anuj have " . $studentpercent ['Anuj'] . " Percent marks in B.Tech.";
?>
Output associativearaay Example2
<?php
$studentpercent = array ("Anuj"=>76, "Sanjeev"=>68, "Rahul"=>90);
echo "This is Second Example of Associative Array" .'<br>'.'<br>';
echo "Rahul have " . $studentpercent ['Rahul'] . " Percent marks in MBA.";
?>
Output associativearray2 Multidimensional Array A Multidimensional array is also known as a two-dimensional array. A Multidimensional array is an array of arrays i.e. 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. A multidimensional array is an array of arrays, which means that the array can contain arrays within itself. If an array element value is another array then this is a multidimensional array. Syntax of Multidimensional Array $arrayname = array ($array1, $array2, $array3) ; Example1
<?php
$myLists['Employee'] = array("1"=>"Anuj Kumar",
 "2"=>"Sanjeev kumar",
 "3"=>"Rahul Chaubey",
 "4"=>"Atul Raguvanshi",
 "5"=>"Ashish Jain");
echo "This is the First Example of Multidimensional Array".'<br>'.'<br>';
echo $myLists ['Employee'][5] . " is a owner of company .";
?>
Output multidimensionarray Example2
<?php
$department = array
(
 "Development"=>array ( "Anuj", "Amit", "Sanjeev" ),
"Designing"=>array ( "Ashish" ),
"Testing"=>array ( "Atul", "Rahul", "Himanshu" )
);
echo "This is the Second Example of Multidimensional Array".'<br>' .'<br>';
echo $department ['Development'][2] . " is a employee of the Development
department.";
?>
Output multidimensionarray2 ]]>

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
.

Recommended Tutorials for you

Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.