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. 

PHP Arrays

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.

Table of Content

  • Types of Array in PHP
  • Indexed or Numeric Arrays
  • Associative Arrays
  • Multidimensional Arrays

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.

Similar Reads

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

Associative Arrays

...

Multidimensional Arrays

...