PHP Indexed Array

In PHP, arrays are linear data structures that allow to store multiple elements of the same type under a single variable which helps in saving the time and effort of creating a distinct variable for each data.

It is helpful to use arrays to compile a list of elements through which their index or key can retrieve easily.

Suppose, we want to store five names and printing them as needed. The usage of five separate string variables makes this simple. However, it would be pretty challenging for the user or developer to generate many independent variables if the number increased from five to one hundred.

In this situation, the use of an array enables us to keep all of the elements in a single variable and makes it simple to retrieve them using an index or a key. In PHP, an array is made using the array() function.

PHP includes a number of built-in functions for dealing with arrays. Arrays, along with these functions, have become a significant feature of PHP.

In PHP, there are essentially three types of arrays:

1. Numeric or Indexed Arrays: An array having a linearly-stored numeric index.

2. Associative Arrays: An array with a string index where each value can be given a particular key in place of linear storage.

3. Multidimensional Arrays: An array that may be accessed via several indices. This type of array contains one or more arrays.

Indexed Array:

An indexed array is a simple array that stores data elements against numeric indexes. Each array element is represented by an index, where 0 is the numeric value of first array element.

Syntax:

There are 2 methods to initialize an array

  1. Direct array initialization
  2. Distributed array initialization

Direct Array Initialization

<?php
            /* 
             1st - direct array initialization
           */
           $arrayname= array("element 1", "element 2", element 3");
  ?>

Distributed Array Initialization

<?php
$arrayname[0] = "element1";
$arrayname[1] = "element2";
$arrayname[2] = "element3";
$arrayname[3] = "element4";
$arrayname[4] = "element5";
?>

Traverse through index array

<?php
// using the for loop
for ($i = 0; $i < $size; $i++)
{
    echo $arrayname[$i], "\n";
}
?>

Examples of the Indexed Array

Program 1: Program to print an array of language names:

<?php  
$languages=array ("java", "PHP", "python", “javaScript”);  
echo "language Name: $language[0],  $language[1] , language[2], language[3]";
  ?>  

The output of the program:

Language name: java, PHP, python and javascript

Program 2: Program to print bike names:

<?php
$bikes=array (“royal Enfield”, “duke”, “ns200”, “xt” );
Echo “bike names: $bike[0], $bike[1], $bike[2], $bike[3]”;
?>

The output of the program:

Bike names: royal Enfield, duke, ns200, xt.

Examples of Distributed array initialization

Program 1: Program to print different course using indexed array:

<?php  
$course [0] ="python";  
$course [1] ="java";  
$course [2] =”PHP”; 
$course [3] =”javascript”;
echo "Course Name: $course [0]”; 
echo “$course [1]”;
echo” $course [2]”;
echo” $course [3]";  
?>  

The output of the program:

Course name: python
             Java
             PHP
             Javascript

Program 2: Program to print different names using indexed array:

<?php
$student [0] = “joe”;
$student [1] = “Janu”;
$student [2] = “Amith”;
$student [3] = “Sundar”;
echo “student names: student [0], student [1], student [2] and student [3] ”;
?>

The output of the program:

Student names: joe, Janu, Amith, Sundar

Example of Accessing PHP indexed array:

<?php
$numbers = array (“1” , “2” , “3” , “4” , “5” , “6” , “7” , “8” , “9”);
// using the for loop
for ($i = 0; $i < 9; $i++)
{
    echo $numbers[$i], "\n";
}
?>

The output of the program:

1
2
3
4
5
6
7
8
9

Program to print array using Foreach loop:

<?php
 // construct an array of data elements//
$primes = array (“2” , “3” , “5” , “7”, “11” );
foreach ( $primes as $primes )
{
 echo “$prime numbers are: $prime<br /> “;
}
?>

The output of the program:

Prime numbers are 2
3
5
7
11

Examples to count length of indexed array:

Program 1:-Program to print the count of array elements:

<?php
$numbers= array (“1” , “2” , “3” , “4” , “5” , “6” , “7” , “8” , “9” )
Echo count ($numbers);
?>

The output of the program:

Count: 9

Program 2: Program to print the count of array elements:

<?php  
$course [0] = "python";  
$course [1] = "java";  
$course [2] = “PHP”; 
$course [3] = “javascript”;
echo count($course);
?>

The output of the program:

Count: 4

Related Topics

PHP htmlspecialchars() Function

PHP htmlspecialchars() Function The htmlspecialchars() function in PHP converts some special predefined characters to HTML entities. The special predefined characters are as follows: & (ampersand) OR &amp;" (double quote) OR &quot;' (single quote) OR &#039;<...

2 minutes read.

PHP stripcslashes() Function

PHP stripcslashes() Function The stripcslashes() function in PHP returns a string with backslashes(\n, \r ..., octal and hexadecimal representation) stripped off. Syntax stripcslashes ( string $str )  Parameter str(required)- This parameter signifies the string to be unescaped. Return This function returns the unescaped string with backslashes stripped off. Example 1   ...

1 minute read.

PHP array_walk() Function

PHP array_walk() Function The array_walk() function in PHP is used to apply the user-defined callback function to each element of the array. This function returns a Boolean TRUE on success or FALSE on failure. Syntax array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] ) Parameter array(required)- This array represents...

1 minute read.

PHP is_real() Function

The PHP is_real () function in PHP is used to find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter...

1 minute read.

PHP array_fill() function

PHP array_fill() function The array_fill() function in PHP fills an user-defined array with num entries of the value that of specified values parameter, where the keys start at the start_index parameter. Syntax array_fill ( int $start_index , int $num , mixed $value ) Parameter start_index(required)- This parameter represents the first index of the...

1 minute read.

PHP end() function

PHP end() function The end() function in PHP sets the internal pointer of an array to its last element. This function returns the value of the last element Syntax end ( array &$array )   Parameter array (required)– This parameter represents the input array. Return This function returns...

1 minute read.

PHP array_count_values() Function

PHP array_count_values() Function The array_count_values() function in PHP counts all the values present in the specified array and returns an array using the values of the array as keys and their frequency in the array as values. Syntax array_count_values ( array $array ) Parameter array(required)- This parameter represents the...

1 minute read.

PHP array_pad() Function

PHP array_pad() Function The array_pad() function in PHP pads the array to the given length and value and returns a copy of the array padded to size and value specified by size and value parameter. The padding takes place as...

1 minute read.

PHP vfprintf() Function

PHP vfprintf() Function The vfprintf() function in PHP writes a formatted string to a specified output stream. Syntax vfprintf ( resource $handle , string $format , array $args )  Parameter handle(required)- This parameter represents where to write the string. format(required)- This parameter specifies the string and how to format the...

3 minutes read.

PHP arsort() function

PHP arsort() function The arsort() function in PHP sorts an array in the reverse order and maintains the index association Syntax arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to modify the behavior...

2 minutes read.

PHP is_countable() Function

The PHP is_countable() function in PHP is used to  find whether a variable is countable or not. It returns a Boolean value true if the parameter var is countable else it returns false. Syntax is_countable ( mixed...

1 minute read.

WordPress Plugin

WordPress Plugin is a process used to add or extend the functionality which can be enabled or disabled, and it does not interfere with the software and its code. WordPress is lightweight and smooth...

2 minutes read.

PHP array_intersect() function

The array_intersect () function in PHP is used to compute the intersection of arrays. It returns an array containing all the entries from array1 that are present in all the other arrays. Syntax array_intersect ( array $array1 , array $array2 [, array $... ]) Parameter array1(required)- This parameter...

1 minute read.

PHP array_keys() Function

PHP array_keys() Function The array_keys() function  in PHP is used to return all the keys, numeric and string from the given array. Syntax array_keys ( array $array ) or array_keys ( array $array , mixed $search_value [, bool $strict = FALSE ] )  Parameter array(required)- This parameter specifies the input array search_value(optional)- This parameter specifies a value, where...

1 minute read.

PHP bin2hex() Function | Convert Binary to Hexadecimal

PHP bin2hex() Function The bin2hex() function in PHP converts the specified string value of ASCII characters to hexadecimal value. Syntax bin2hex(str) Parameter str(Required): This parameter represents the string to be converted into hexadecimal. Return  This function returns...

1 minute read.

PHP str_word_count() Function

PHP str_word_count() Function The str_word_count() function in PHP counts the number of words inside the string and returns the information about words used in a string. String str_word_count( string $string [, int $format [, string $charlist ]] ) Parameter string(required)- This parameter represents the input string. format(required)- This parameterspecifies the...

2 minutes read.

PHP addslashes() Function

PHP addslashes() Function The addslashed() function in PHP is used to return a string with backslashes in front of predefined characters. This function is only used with some characters which are...

1 minute read.

PHP array_diff_assoc() Function

PHP array_diff_assoc() Function The array_diff_assoc() Function in PHP compares the difference of arrays and returns an array containing all the values from array1 that are not present in any of the other arrays. Syntax array_diff_assoc ( array $array1 , array $array2 [, array $... ] ) Parameter array1(required)- This parameter signifies...

1 minute read.

PHP is_double() Function

The PHP is_double() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

PHP natcasesort() Function

PHP natcasesort() Function The natcasesort() function in PHP Sort an array using a case insensitive "natural order" algorithm while maintaining the keys. Syntax natcasesort ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns a Boolean...

1 minute read.