Arrays in PHP

In simple words, an Array is a variable that makes us able to store more than one same kind of value 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 a 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 in PHP 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 the student Array. Then you have to add values like the name of the first student on the 0 indexes of the student array, and the name of the next student on the 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 the 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 a 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 the detailed structure of the array. There is another function var_dump() you can use this. Check the 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 arrays in PHP. We will try to understand each type of array using Examples. These three types are as follows

  • Indexed array
  • Associative array
  • Multidimensional array

Indexed array:

Indexed Arrays are simple arrays in which we save values on the index using the 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:

In an associative array, we save values in the 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 an array on the indexes of an 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
Scroll to Top