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.

print_r($a)

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

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

echo '<pre>';
print_r($a);
echo '</pre>';		

Result:

Array
	(
		[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.

$i=0;
while($i < 4){
	echo $a[$i];
	$i++;
}

To make it dynamic

$i=0;
while($i < count($a)){
	echo $a[$i];
	$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.

Example

$a[
	['ram',23,'ktm',20000],
	['shyam',21,'bkt',30000],
	['hari',27,'ptn',40000]
]

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

Example

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

Example

$a = ['name'=>'ram','email'=>'ram@gmail.com','password'=>'ram001']; 
print_r($a);
echo $a['email'];

multidimesional associative array

Example

$a = [
		['name'=>'ram','email'=>'ram@gmail.com','password'=>'ram001'],
		['name'=>'shyam','email'=>'shyam@gmail.com','password'=>'shyam001'],
		['name'=>'hari','email'=>'hari@gmail.com','password'=>'hari001']
	]
print_r($a);
echo $a[1]['email']

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

<table>
	<thead>
		<tr>
			<td>Name</td>
			<td>Email</td>
			<td>Password</td>
		</tr>	
	</thead>
	<tbody>
<?php 
foreach($a as $d){
?>	
		<tr>
			<td><?= $d['name']; ?></td>
			<td><?= $d['email']; ?></td>
			<td><?= $d['password']; ?></td>
		</tr>
<?php } ?>		
	</tbody>
</table>

0 Like 0 Dislike 0 Comment Share

Leave a comment