Array – array is a linear data structure which stores same data elements into contiguous way.
Let’s say, we have an array of 4 elements. The elements are 22, 34,-65, 8. Stored elements into array cells or block.
Ans – So, size/length of the array will = 4.
The graph of array will –
Elements 22 34 -65 8
Index 0 1 2 3
Create an Array in PHP
In PHP, the array() function is used to create an array:
In PHP, there are three types of arrays:
- Indexed arrays – Arrays with a numeric index
- Associative arrays – Arrays with named keys
- Multidimensional arrays – Arrays containing one or more arrays
In array we have many built-in functions.
Note: gettype() -> helps to identify the data type of variable
File – code1.php
<?php $arr=[10,20,30]; echo “type is: “.gettype($arr); $brr=array(10,20,30); echo “<br>type is: “.gettype($brr); ?> |

Write a code, to get the length of an integer & string array.
File – code2.php
<?php $IntArr = [10,30,-4,5,200]; echo ‘integer array length: ‘.count($IntArr); $strArr=array(‘windows’,’linux’,’mac’); echo ‘<br>string array length: ‘.count($strArr); ?> |

- Indexed array
All elements of array are represented by an index number which starts from 0. PHP indexed array can store numbers, strings or any object. PHP indexed array is also known as numeric array.
Write a PHP code, to traverse an integer indexed array.
File – code3.php
<?php $arr=[100,200,250,300,350]; echo ‘array is: ‘; foreach($arr as $i){ echo ‘<br>’.$i; } ?> |

Write a PHP code, to traverse an string indexed array.
File – code4.php
<?php $brr=[‘arjun’,’krishna’,’nakul’,’sahadeb’,’bhim’]; echo “array is: “; for($i=0;$i<count($brr);$i++){ echo “<br>index is: $i”; echo “<br>”.$brr[$i]; } ?> |

- Associative array
An associative array in PHP represents an ordered map. A map is a data form that associates keys with values. This form is well-suited to a variety of tasks; it can be used as an array, list (vector), a hash table (a map implementation), dictionary, set, stack, queue, and possibly more.
Write a PHP code, to traverse an integer indexed array.
File – code5.php
<?php $arr=[0=>100,1=>200,2=>300,4=>400]; foreach($arr as $i=>$j){ echo “<br>$i”; echo “<br>$j”; } ?> |

Write a PHP code, to traverse a string indexed array.
File – code6.php
<?php $arr=array(‘shimanta’=>21,’pritha’=>22,’trina’=>19); echo “array is: “; foreach($arr as $i=>$j){ echo “<br>key -> “.$i; echo “<br>value -> “.$j; } ?> |

- Multidimensional Array
PHP multidimensional array is also known as array of arrays. It allows you to store tabular data in an array. PHP multidimensional array can be represented in the form of matrix which is represented by row * column.
Write a PHP code, to traverse an integer multidimensional array.
File – code7.php
<?php $arr=[[100,200],[30,40]]; echo “2D array is: “; for($i=0;$i<2;$i++){ echo “<br>”; for($j=0;$j<2;$j++){ echo “\t”.$arr[$i][$j]; } } ?> |

Write a PHP code, to traverse a string multidimensional array.
File – code8.php
<?php error_reporting(0); $arr=[[‘shimanta’,’pritha’,’trina’],[‘babai’,’mon’,’butu’]]; echo “string 2D array: “; for($i=0;$i<3;$i++){ echo “<br>”; for($j=0;$j<3;$j++){ // echo “<br>i is $i”; // echo “<br>j is $j”; echo ” “.$arr[$i][$j]; } } ?> |
