your image

PHP Indexed Array - javatpoint

javapoint
Related Topic
:- PHP programming languages

PHP Indexed Array

PHP indexed array is an array which is represented by an index number by default. All elements of array are represented by an index number which starts from 0.

PHP indexed array can store numbers, strings or any object. PHP indexed array is also known as numeric array.

Definition

There are two ways to define indexed array:

1st way:

 

1. Build a CMS using OOP PHP tutorial | PHP MVC design pattern [2020]

  1. $size=array("Big","Medium","Short");  

2nd way:

  1. $size[0]="Big";  
  2. $size[1]="Medium";  
  3. $size[2]="Short";  

PHP Indexed Array Example

File: array1.php

  1. <?php  
  2. $size=array("Big","Medium","Short");  
  3. echo "Size: $size[0], $size[1] and $size[2]";  
  4. ?>  

Output:

Size: Big, Medium and Short

File: array2.php

  1. <?php  
  2. $size[0]="Big";  
  3. $size[1]="Medium";  
  4. $size[2]="Short";  
  5. echo "Size: $size[0], $size[1] and $size[2]";  
  6. ?>  

Output:

Size: Big, Medium and Short

Traversing PHP Indexed Array

We can easily traverse array in PHP using foreach loop. Let's see a simple example to traverse all the elements of PHP array.

File: array3.php

  1. <?php  
  2. $size=array("Big","Medium","Short");  
  3. foreach( $size as $s )  
  4. {  
  5.   echo "Size is: $s<br />";  
  6. }  
  7. ?>  

Output:

Size is: BigSize is: MediumSize is: Short

Count Length of PHP Indexed Array

PHP provides count() function which returns length of an array.

  1. <?php  
  2. $size=array("Big","Medium","Short");  
  3. echo count($size);  
  4. ?>  

Output:

3

Comments