Arrays in PHP

In this tutorial, we will learn about Arrays in PHP. In simple words, an Array is variable which makes us able to store more than one same kind values using a single variable name. For example, you want to save the name of a hundred students in variables. It means you have to make 100 variables. But it makes your code so complicated and It is also time-wasting. Instead of spending so much time on making variables, you can store all 100 names of students in single variable using an array. Here we will learn the basics of array-like how to define an Array and how to Initialize an Array etc.

In an Array values store using a single variable name. But we use the index number to add or fetch the value from Array. For example, you want to store the name of a hundred students in student Array. Then you have to add values like name of the first student on the 0 indexes of student array, and name of next student on 1st index like this.

Defining and Initializing an Array:

Defining an Array in PHP is so simple. The simplest way to define an array using array() function. Check the example below to understand it clearly.

<?php
// Defining and Initializing an Array
$students = array("Ali", "Sameer", "Mohan");
?>

Fetching Values from Array:

Fetching values from the array is so easy there is print_r() function. By using this function you can fetch values and structure of the array.

<?php
// Defining and Initializing an Array
$students = array("Ali", "Sameer", "Mohan");

// Display the students array
print_r($students);
?>

In case you want to fetch values from the array and see detail structure of the array. There is another function var_dump() you can use this. Check program below to check var_dump();

<?php
// Defining and Initializing an Array
$students = array("Ali", "Sameer", "Mohan");

// Display the students array
var_dump($students);
?>

Types of Array in PHP:

There is a total of three types of array in PHP. We will try to understand each type of array using Examples. These three types are as follow

  • Indexed array
  • Associative array
  • Multidimensional array

Indexed array:

Indexed Arrays are a simple array in which we save values on index using array() function as we saw in our previous examples.

<?php
// Defining and Initializing an Array
$students = array("Ali", "Sameer", "Mohan");

$marks[0]=80;
$marks[1]=90;
$marks[3]=85;

print_r($students);
print_r($marks);

?>

Associative array:

An associative array, we save vales in key-value form. For example “Ali”=>80, “Sameer”=>90, “Mohan”=>85. Check this program

<?php
// Defining and Initializing an Array
$stumarks = array("Ali"=-->80, "Sameer"=>90, "Mohan"=>85);

$marks["Ali"]=80;
$marks["Sameer"]=90;
$marks["Mohan"]=85;

print_r($stumarks);
print_r($marks);

?>

Multidimensional array:

Multidimensional Arrays are also known as an array of arrays. Mean instead of storing values on the index of an Array we save array on the indexes of array. Check the following example.

<?php
$studentInfo = array(
    array(
        "name" =--> "ali",
        "marks" => 80,
    ),
    array(
        "name" => "Sameer",
        "marks" => 90,
    ),
    array(
        "name" => "Mohan",
        "marks" => 85,
    )
);

print_r($studentInfo);
?>
Spread the love