php arrays

Array is also a variable. The only difference is array can store multiple values in a single variable. When we fetch multiple data from database array allows us to store them in a single variable rather than creating separate variable for each values. Array can store multiple data types as well. Array can't be printed using echo() but single value from an array can be printed using it. Array can be declared in two ways in php.

1) $a = array(1,2,'ram');

2) $a = [1,2,3,'ram'];

Types of Array

Indexed arrays - Arrays with a numeric index

Associative arrays - Arrays with named keys

Multidimensional arrays - Arrays containing one or more arrays

Indexed Array

The values of an indexed array are referenced via indexed number starting from 0. Indexed array can further be subdivided into two categories.

Single dimensional Indexed Array

Multi-dimensional Indexed Array

Single Dimensional

Single dimensional indexed array has single level values inside an array.

$a = array(16,23,'ram');
echo $a[2] . 'Your roll number is' . $a[1];

print_r() command can be used to print all values of single dimensional array.


Result: Array([0] => 16 [1] => 23 [2] => ram)

We can use pre tag as well to get it in preformatted structure.

echo '<pre>';
echo '</pre>';		


		[0] => 16 
		[1] => 23 
		[2] => ram

We can count the values in an array using count() or sizeof() function as well.

echo count($a);	

Adding value in such array as the last index can be done using [] behind the variable.

$a[] = 'hello'; 	

Replacing values in the array can also be done the same way defining the index number in big braces.

$a[2] = 'ramesh';

To print all values of an array using loop.

while($i < 4){
	echo $a[$i];

To make it dynamic

while($i < count($a)){
	echo $a[$i];

Foreach loop is very useful while looping through arrays easily. $key holds the index of arrray while $value holds the value of array.

foreach ($a as $key as $value){
	echo $key . $value;

If the key is not needed you can exclude $key from the syntax.

foreach ($b as $value){
	echo $value;

Multi-dimensional Array

Multidimensional array is a multilevel array or nested array.



We need to call the index of main array as well as the internal array to print values from such arrays.


echo 'The sum of' .$a[0][0] .'and'. $a[1][0] .'\'s salary is' .($a[0][3] + $a[1][3]); 

Printing all values from multidimensional array.

for($i = 0;$i < 3; $i++){
	for($j = 0;$j < 4; $j++){
		echo $a[$i][$j];

More dynamic approach

for($i = 0;$i < count($a); $i++){
	for($j = 0;$j < count($a[$i]); $j++){
		echo $a[$i][$j];

associative array

Associative arrays are arrays associated with user defined keys.

Single dimesional associative array


$a = ['name'=>'ram','email'=>'','password'=>'ram001']; 
echo $a['email'];

multidimesional associative array


$a = [
echo $a[1]['email']

We can print such values using loop and display in a table easily.

foreach($a as $d){
			<td><?= $d['name']; ?></td>
			<td><?= $d['email']; ?></td>
			<td><?= $d['password']; ?></td>
<?php } ?>