All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Arrays

PHP Array is used to store similar multiple type of values in a single variable.PHP has three different type of arrays Numeric Array, Associative Array, Multidimensional Array.



Creating An Array

You can create array by using array() function or long method


array();

/*Long Method*/
$val[0]=1;
$val[1]=2;
$val[2]=3;
$val[3]=4;

Numeric Array

Numeric Array are those array which are used to store any kind of value like number, string etc. By default array index starts from zero.


$val=array(2324, 434, 45, 4849);

foreach($val as $array)
{
  echo $array;
}



Associative Array

Associative array will have their index as string so that you can establish a strong association between key and values.


$rollno = array("Aman"=>"1", "Rahul"=>"21", "Karan"=>"16");

foreach($rollno as $array)
{
  echo $array;
}

/*Or you can use long method*/
echo $rollno['Aman'];
echo $rollno['Rahul'];
echo $rollno['Karan'];



Multidimensional Array

Multidimensional array are used to make array in array like 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. Values in the multi-dimensional array are accessed using multiple index.


$student_info = array( 
	"aman" => array
	(
	 "rollno" => 2,	    
	 "course" =>"B-Tech",	    
	),
	"rahul" => array
	(
	 "rollno" => 21,	    
	 "course" => "BCA",	    
	),
	"karan" => array
	(
	 "rollno" => 16,	    
	 "course" =>"MCA",	    
	),
);

echo $rollno['aman'][course];
echo $rollno['rahul'][course];
echo $rollno['karan'][course];
❮ PrevNext ❯