Array in PHP| Different Type of Array in PHP| How to use Array in PHP

An array is a data structure that stores one or more similar types of values in a single value.

Suppose you want to print 10 numbers. Now, instead of defining 10 different variables, we can create 1 array in which we can store or print 10 numbers.

Types of arrays

Indexed array

Index number work on 0, 1, 2. The index number of an array always starts from number 0. Suppose we have 3 values A, B, C and you want to count these values. The index will count these values from numbers 0, 1, 2. That means A will print at 0 index. B at 1 index and C at 2 index number.

Syntax of index array:

$array_name=array(“value”, “value”, “value”,……..)

<?php $name = ["amit", "sumit", "ramit"];
echo "Names are: $name[0], $name[1], $name[2]"; ?>

Output

Names are: amit, sumit, ramit

Method II

<?php
$name[0] = "amit";
$name[1] = " sumit ";
$name[2] = " ramit ";
echo "Names are: $name[0], $name[1], $name[2]";
?>

Output

Names are: amit, sumit , ramit

Traversing PHP Indexed Array

Traversing PHP index array just works like shorthand property to get array values. It is used with foreach loop

Syntax:

foreach ($array as $value)

Program

<?php 
$month = ["Jan", "Feb", "March"];
foreach ($month as $m) {
    echo "Month is: $m<br />";
} 
?>

Output

Month is: Jan 
Month is: Feb 
Month is: March

Count Length of PHP Indexed Array

PHP provides count() function which returns length of an array.

Example

<?php 
$name = ["rohan", "sohan", "mohan"];
echo count($name); 
?>

Output

3

Associative arrays

Associative arrays work with 2 parameters. One is Key and the other one is Value. we write them as Key=>Value

Method I

$salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");

Program

<?php 
$age = ["Amit" => "35", "Sumit" => "45", "Ramit" => "20"];
echo "Amit age: " . $age["Amit"] . "<br/>";
echo "Sumit age: " . $age["Sumit"] . "<br/>";
echo "Ramit age: " . $age["Ramit"] . "<br/>"; 
?>

Output

Amit age: 35 
Sumit age: 45 
Ramit age: 20

Method II

$salary["Sonoo"]="350000";
$salary["John"]="450000";
$salary["Kartik"]="200000";

Program

<?php
$age["Amit"] = "35";
$age["Sumit"] = "45";
$age["Ramit"] = "20";
echo "Amit age: " . $age["Amit"] . "<br/>";
echo "Sumit age: " . $age["Sumit"] . "<br/>";
echo "Ramit age: " . $age["Ramit"] . "<br/>";
?>

Output

Amit age: 35 
Sumit age: 45 
Ramit age: 20

Traversing PHP Associative Array

Shorthand property to write PHP associative array

Program

<?php
$salary = ["Amit" => "550000", "Sumit" => "250000", "Ramit" => "200000"];
foreach ($salary as $k => $v) {
    echo "Key: " . $k . " Value: " . $v . "<br/>";
} 
?>

Output

Key: Amit Value: 550000
Key: Sumit Value: 250000
Key: Ramit Value: 200000

PHP Multidimensional or Nested Array

Multidimensional arrays are an array of arrays. That means there are multiple arrays in one single array. It shows data in rows and columns. A multidimensional array uses a loop statement to print an array in tabular form.

Example

$id = array ( array(1,"sonoo",400000), array(2,"john",500000), array(3,"rahul",300000) );

Example of PHP Multidimensional Array

Suppose you want to display your values in rows and columns. So below example will show you how to do that.

Multidimensional indexed array

Example

$emp = array
(
array(1,"rohan",400000),
array(2,"sohan",500000),
array(3,"mohan",300000)
);

As we have discussed in an indexed array that every array starts with the number 0. So, in the above example of a multidimensional array, we have 3 rows and 3 columns to print our data. So, in below table 0, 1, 2 is defined as array numbers for rows and columns. Suppose we want to print 2nd row and 2nd column value i.e. 2 Sohan. So, we will print our echo as echo $emp [1] [1]. So same we will print all the values by using a loop statement.

Program

Working of a multidimensional array:

  • In step 1, user will define a variable e.g. &emp=array
  • In step 2, arrays of an array are defined as mentioned in the program given above.
  • In step 3, we have set a loop statement for both rows and columns.
  • We have defined 2 variables named $row for the outer loop and $col for the inner loop. You can give any name to these variables. Here for loop will start working and the user will get its output like below. The working of for loop has already been explained earlier in loop chapters.

Output

1 rohan 400000 
2 sohan 500000 
3 mohan 300000

Array in PHP| Different Type of Array in PHP| How to use Array in PHP
Show Buttons
Hide Buttons