Tutorialspoint.dev

PHP | Arrays

Arrays in PHP is a type of data structure that allows us to store multiple elements of similar data type 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 hundred, then it would be really difficult for the user or developer to create so much 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.

There are basically three types of arrays 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 which contains single or multiple array 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 elements, but an index is always a number. By default, the index starts at zero. These arrays can be created in two different ways as shown in the following example:

<?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: ";
echo $name_one[2], " ";
echo $name_one[0], " ";
echo $name_one[4], " ";
  
// 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: ";
echo $name_two[2], " ";
echo $name_two[0], " ";
echo $name_two[4], " ";
  
?>

Output:



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

Traversing: We can traverse an indexed array using loops in PHP. 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.
Example:

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

Output:

Looping using foreach: 
Zack
Anthony
Ram
Salim
Raghav

The number of elements are 5 
Looping using for: 
ZACK
ANTHONY
RAM
SALIM
RAGHAV

Associative Arrays

These type 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.

Example:

<?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: ";
echo $name_two["zack"], " ";
echo $name_two["salim"], " ";
echo $name_two["anthony"], " ";
echo $name_one["Ram"], " ";
echo $name_one["Raghav"], " ";
  
?>

Output:

Accessing the elements directly:
zara
sara
any
Rani
Ravina

Traversing Associative Arrays: We can traverse associative arrays in a similar way did in numeric arrays using loops. 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.
Example:

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

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

Multidimensional Arrays

Multi-dimensional arrays are such arrays which stores an another array at each index instead of single element. In other words, we can define multi-dimensional arrays as 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.
Example:



<?php
  
// Defining a multidimensional array
$favorites = array(
    array(
        "name" => "Dave Punk",
        "mob" => "5689741523",
        "email" => "[email protected]",
    ),
    array(
        "name" => "Monty Smith",
        "mob" => "2584369721",
        "email" => "[email protected]",
    ),
    array(
        "name" => "John Flinch",
        "mob" => "9875147536",
        "email" => "[email protected]",
    )
);
  
// Accessing elements
echo "Dave Punk email-id is: " . $favorites[0]["email"], " ";
echo "John Flinch mobile number is: " . $favorites[1]["mob"];
  
?>

Traversing Multidimensional Arrays: We can traverse through the multidimensional array using for and foreach loop in a nested way. That is, one for loop for the outer array and one for loop for the inner array.

Example:

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

Output:

Dave Punk
mob : 2584369721
email : [email protected]

John Flinch
mob : 9875147536
email : [email protected]

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

1 Comments

load comments

Subscribe to Our Newsletter