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 str_rot13() Function

PHP str_rot13() Function The str_rot13() function in PHP performs the ROT13 encoding on a string and returns the resulting string. The ROT13 encoding is used to shift every letter by 13 places in the alphabet...

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 quotemeta() Function

PHP quotemeta() Function The quotemeta() function in PHP quotes the meta characters and returns a string with a backslash character (\) before some predefine characters. The predefined characters are as follows: period ...

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.

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 Arrays

PHP Array is usedto hold more than one value at a time.  In another words, we can say that an array stores multiple values in one single variable. Following are the...

2 minutes read.

PHP strcoll() Function

PHP strcoll() Function The strcoll() function in PHP compares two strings. This function is case-sensitive but not binary-safe. It uses the current locale for doing the comparisons. Syntax strcoll ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter...

1 minute read.

PHP Include Function

PHP Include( ) The PHP include() statement holds all the code/text that liesin the defined file and paste it into the file that uses the "include" statement. With the help of include...

3 minutes read.

PHP Magic Constants

There are various predefine PHP Magic Constants. It starts with underscore(__) and ends with double underscore.  Magic Constant Description __LINE__ To check current line number of the file. __FILE__ To check full path and filename of...

1 minute read.

PHP array_fill_keys() Function

The array_fill_keys() function in PHP fills an array with the value of the specified value parameter, using the values of the specified keys array as keys. Syntax array_fill_keys ( array $keys , mixed $value ) Parameter keys(required)- This parameter signifies the input array of values that will be...

1 minute read.

PHP array_rand() Function

PHP array_rand() Function The array_rand() function in PHP picks one or more random keys out of an array. Syntax array_rand ( array $array [, int $num = 1 ] ) Parameter array(required)- This parameter represents the input array. num(optional)- This parameter specifies how many entries should be picked....

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_reverse() Function

PHP array_reverse() Function The array_reverse() function in PHP returns a new array with the elements in the reversed order. Syntax array_reverse (array $array [, bool $preserve_keys = FALSE ] ) Parameter array(required)- This parameter represents the input array preserve_keys (optional)- This parameter specifies if the function should preserve the keys...

1 minute read.

PHP array_chunk() Function

PHP array_chunk() Function The array_chunk() function in PHP is used to split an array into chunks where the last chunk may contain less than size elements. Syntax array_chunk ( array $array , int $size [, bool $preserve_keys ]...

1 minute read.

PHP array_column() Function

PHP array_column() Function The array_column() function in PHP is used to return the values from a single column specified in the input array. Syntax array_column (array $input, mixed $column_key [, mixed $index_key]) Parameter input(required)- This parameter represents a multi-dimensional array or an array of...

1 minute read.

PHP array_unique() Function

PHP array_unique() Function The array_unique() function in PHP removes the duplicate values from the specified array. Syntax array_unique (array $array [, int $sort_flags = SORT_STRING]) Parameter array(required)- This array represents the first input array. sort_flags (optional)- This parameter is used to modify the sorting behavior. The...

1 minute read.

PHP uksort() Function

PHP uksort() Function The uksort() function in PHP sorts an array by keys using a user-defined comparison function. Syntax uksort ( array &$array , callable $key_compare_func ) Parameter array(required)- This parameter represents the input array. key_compare_func(required)- This parameter represents a string that describe a callable comparison function....

1 minute read.

PHP str_replace() Function

PHP str_replace() Function The str_replace() function in PHP replaces all occurrences of the search string with the specified replacement string. This function is case-sensitive. Syntax str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter signifies the value to search. replace(required)-...

1 minute read.

PHP Array Functions

There are various array functions in PHP. They are used to access and manipulate the elements ofan array. Following are functions of array. Array Function Description array() It is used to create an array. array_change_key_case() It is...

3 minutes read.

PHP strnatcmp() Function

PHP strnatcmp() Function The strnatcmp() function in PHP compares two strings using a "natural" algorithm. It is case-sensitive. Syntax strnatcmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than(...

1 minute read.