Mainly two types of Array in PHP
- SINGLE ARRAY/ SINGLE DIMATIONAL ARRAY
- MULTIPLE ARRAY/ MULTI-DIMAINTIONAL ARRAY
This two types of the array divided into two types of INDEX ARRAY/ NUMERIC ARRAY & ASSOCIATIVE ARRAY.
Single Array
In the single array, we can store multiple values in one variable.
Example
1 2 3 4 | <?php $fruits = [‘apple’,’mango’,’banana’,’lemon’]; print_r($fruits); ?> |
Like a fruits array store all fruits name.
All previous examples are single array.
Numeric Array / Index Array
In the numeric array, all index value must be integer or number. If you only store data in an array, index automatically starts with zero this kind of array is called numeric or index array. You don’t need to assign index.
But if you want to address the index value you can, the index must be assigned with integer type of value.
Example
1 2 3 4 5 6 7 8 9 | <?php //This is the example of manual index value. We can also use in this manner. $fruit[2] = ‘Apple’; $fruit[5] = ‘Banana’; $fruit[10] = ‘Coconut’; $fruit[25] = ‘Mango’; $fruit[30] = ‘Strawberry’; print_r($fruit); ?> |
Associative Array
If you assign index value with string and each element can be assigned by unique key this is called Associative array.
1 2 3 | <?php $employee = [‘name’=>’Bob’,’address’=>’USA’,’age’=>34,’designation’=>’Developer’]; ?> |
Above example is an associative array.
Benefit of this array is we can easily recognize the details of the employee or anything (which you store).
We can also store these details in numeric array
1 | <?php $employee = [’Bob’,’USA’,34,’Developer’]; ?> |
But you don’t know which value is the name of this employee, which value is the address of this employee and also same as age and designation.
But in the associative array, we easily find the employee name by its index value which declared by name.
print_r(), var_dump() and foreach loop used to print an associative array.
1 | Example |
1 2 3 4 5 6 | <?php $employee = [‘name’=>’Bob’,’address’=>’USA’,’age’=>34,’designation’=>’Developer’]; foreach($employee as $key=>$value){ echo “$key - $value<br>”; } ?> |
Output
1 2 3 4 | name – Bob address – USA age – 34 designation - Developer |
Multi-dimensional Array or Multiple Array
In multiple array we can store more than one array in a single variable. So we can manage more than one array in a single stack.
Like we can store all employee details in a single variable
1 2 3 4 5 6 7 8 9 10 11 | <?php $employees = [ 0=>[‘name’=>’Bob’,’city’=>’USA’,’age’=>34,’designation’=>’Developer’], 1=>[‘name’=>’Tom’,’city’=>’GERMANY’,’age’=>45,’designation’=>’Team Leader’], 2=>[‘name’=>’Herry’,’city’=>’FRANCE’,’age’=>28,’designation’=>’Tester’], 3=>[‘name’=>’Jhon’,’city’=>’BRAZIL’,’age’=>43,’designation’=>’Project Manager’], 4=>[‘name’=>’Jack’,’city’=>’CNADA’,’age’=>32,’designation’=>’Designer’], ]; //this is a multi-dimensional array print_r($employees); ?> |
Output
1 | array(0=>array(name=>Bob city=>USA age=>34 designation=>Developer) 1=>array(name=>Tom city=>GERMANY age=>45 designation=>Team Leader) 2=>array(name=>Harry city=>FRANCE age=>28 designation=>Tester) 3=>array(name=>John city=>BRAZIL age=>43 designation=>Project Manager) 4=>array(name=>Jack city=>CANADA age=>32 designation=>Designer)) |
The main array ‘employees’ have 5 index each index store a single array.
This is also two dimentional array because you can return any employee details by two indexes.
Like if we want to display the second employee’s designation just print
1 2 3 | <?php echo $employees[1][designation]; ?> |
Result
1 | ‘Team Leader’ |
In this way you can create multi-dimensional arrays and store multiple array in one single variable.