Open In App

PHP Arrays

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Arrays in PHP are a type of data structure that allows us to store multiple elements of similar or different data types under a single variable thereby saving us the effort of creating a different variable for every data. The arrays are helpful to create a list of elements of similar types, which can be accessed using their index or key.

Suppose we want to store five names and print them accordingly. This can be easily done by the use of five different string variables. But if instead of five, the number rises to a hundred, then it would be really difficult for the user or developer to create so many different variables. Here array comes into play and helps us to store every element within a single variable and also allows easy access using an index or a key. An array is created using an array() function in PHP.

Types of Array in PHP

  • Indexed or Numeric Arrays: An array with a numeric index where values are stored linearly.
  • Associative Arrays: An array with a string index where instead of linear storage, each value can be assigned a specific key.
  • Multidimensional Arrays: An array that contains a single or multiple arrays within it and can be accessed via multiple indices.

Indexed or Numeric Arrays

These type of arrays can be used to store any type of element, but an index is always a number. By default, the index starts at zero. These arrays can be created in two different ways.

Examples of Indexed or Numeric Arrays

Example 1:

PHP




<?php
 
// One way to create an indexed array
$name_one = array("Zack", "Anthony", "Ram", "Salim", "Raghav");
 
// Accessing the elements directly
echo "Accessing the 1st array elements directly:\n";
echo $name_one[2], "\n";
echo $name_one[0], "\n";
echo $name_one[4], "\n";
 
// Second way to create an indexed array
$name_two[0] = "ZACK";
$name_two[1] = "ANTHONY";
$name_two[2] = "RAM";
$name_two[3] = "SALIM";
$name_two[4] = "RAGHAV";
 
// Accessing the elements directly
echo "Accessing the 2nd array elements directly:\n";
echo $name_two[2], "\n";
echo $name_two[0], "\n";
echo $name_two[4], "\n";
 
?>


Output: 

Accessing the 1st array elements directly:
Ram
Zack
Raghav
Accessing the 2nd array elements directly:
RAM
ZACK
RAGHAV

Example 2:  We can traverse an indexed array using loops in PHP.

PHP




<?php
 
// Creating an indexed array
$name_one = array("Zack", "Anthony", "Ram", "Salim", "Raghav");
 
// One way of Looping through an array using foreach
echo "Looping using foreach: \n";
foreach ($name_one as $val){
    echo $val. "\n";
}
 
// count() function is used to count
// the number of elements in an array
$round = count($name_one);
echo "\nThe number of elements are $round \n";
 
// Another way to loop through the array using for
echo "Looping using for: \n";
for($n = 0; $n < $round; $n++){
    echo $name_one[$n], "\n";
}
 
?>


Output: 

Looping using foreach: 
Zack
Anthony
Ram
Salim
Raghav
The number of elements is 5 
Looping using for: 
ZACK
ANTHONY
RAM
SALIM
RAGHAV

Traversing: We can loop through the indexed array in two ways. First by using for loop and secondly by using foreach. You can refer to PHP Loops for the syntax and basic use. 

Associative Arrays

These types of arrays are similar to the indexed arrays but instead of linear storage, every value can be assigned with a user-defined key of string type. 

Examples of Associative Arrays

Example 1:  

PHP




<?php
 
// One way to create an associative array
$name_one = array("Zack"=>"Zara", "Anthony"=>"Any",
                  "Ram"=>"Rani", "Salim"=>"Sara",
                  "Raghav"=>"Ravina");
 
// Second way to create an associative array
$name_two["zack"] = "zara";
$name_two["anthony"] = "any";
$name_two["ram"] = "rani";
$name_two["salim"] = "sara";
$name_two["raghav"] = "ravina";
 
// Accessing the elements directly
echo "Accessing the elements directly:\n";
echo $name_two["zack"], "\n";
echo $name_two["salim"], "\n";
echo $name_two["anthony"], "\n";
echo $name_one["Ram"], "\n";
echo $name_one["Raghav"], "\n";
 
?>


Output: 

Accessing the elements directly:
zara
sara
any
Rani
Ravina

Example 2:  We can traverse associative arrays in a similar way did in numeric arrays using loops.

PHP




<?php
 
// Creating an Associative Array
$name_one = [
    "Zack" => "Zara",
    "Anthony" => "Any",
    "Ram" => "Rani",
    "Salim" => "Sara",
    "Raghav" => "Ravina",
];
 
// Looping through an array using foreach
echo "Looping using foreach: \n";
foreach ($name_one as $val => $val_value) {
    echo "Husband is " . $val . " and Wife is " . $val_value . "\n";
}
 
// Looping through an array using for
echo "\nLooping using for: \n";
$keys = array_keys($name_one);
$round = count($name_one);
 
for ($i = 0; $i < $round; ++$i) {
    echo $keys[$i] . " " . $name_one[$keys[$i]] . "\n";
}
?>


Output: 

Looping using foreach: 
Husband is Zack and Wife is Zara
Husband is Anthony and Wife is Any
Husband is Ram and Wife is Rani
Husband is Salim and Wife is Sara
Husband is Raghav and Wife is Ravina
Looping using for: 
zack zara
anthony any
ram rani
salim sara
raghav ravina

Traversing Associative Arrays: We can loop through the associative array in two ways. First by using for loop and secondly by using foreach. You can refer to PHP Loops for the syntax and basic use. 

Multidimensional Arrays

Multi-dimensional arrays are such arrays that store another array at each index instead of a single element. In other words, we can define multi-dimensional arrays as an array of arrays. As the name suggests, every element in this array can be an array and they can also hold other sub-arrays within. Arrays or sub-arrays in multidimensional arrays can be accessed using multiple dimensions.

Examples of Multidimensional Arrays

Example 1:  

PHP




<?php
 
// Defining a multidimensional array
$favorites = array(
    array(
        "name" => "Dave Punk",
        "mob" => "5689741523",
        "email" => "davepunk@gmail.com",
    ),
    array(
        "name" => "Monty Smith",
        "mob" => "2584369721",
        "email" => "montysmith@gmail.com",
    ),
    array(
        "name" => "John Flinch",
        "mob" => "9875147536",
        "email" => "johnflinch@gmail.com",
    )
);
 
// Accessing elements
echo "Dave Punk email-id is: " . $favorites[0]["email"], "\n";
echo "John Flinch mobile number is: " . $favorites[2]["mob"];
 
?>


Output: 

Dave Punk email-id is: davepunk@gmail.com
John Flinch mobile number is: 9875147536

Example 2:  We can traverse through the multidimensional array using for and foreach loop in a nested way.

PHP




<?php
// Defining a multidimensional array
$favorites = array(
    "Dave Punk" => array(
        "mob" => "5689741523",
        "email" => "davepunk@gmail.com",
    ),
    "Dave Punk" => array(
        "mob" => "2584369721",
        "email" => "montysmith@gmail.com",
    ),
    "John Flinch" => array(
        "mob" => "9875147536",
        "email" => "johnflinch@gmail.com",
    )
);
 
// Using for and foreach in nested form
$keys = array_keys($favorites);
for($i = 0; $i < count($favorites); $i++) {
    echo $keys[$i] . "\n";
    foreach($favorites[$keys[$i]] as $key => $value) {
        echo $key . " : " . $value . "\n";
    }
    echo "\n";
}
 
?>


Output: 

Dave Punk
mob : 2584369721
email : montysmith@gmail.com
John Flinch
mob : 9875147536
email : johnflinch@gmail.com

Traversing Multidimensional Arrays: That is, one for loop for the outer array and one for loop for the inner array. 

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

 



Last Updated : 11 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads