If you have already register Login here.
PHP indexed array is an array that is represented by an index number by default. All elements of an 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 a numeric array.
There are two method to define indexed array:
First Method :
$colors = array("Red", "Blue", "Yellow");
Second Method :
$colors[0]="Red";
$colors[1]="Blue";
$colors[2]="Yellow";
File Name: first_array.php
<?php
$colors=array("Red","Blue","Yellow");
echo "Color Name : $colors[0], $colors[1] and $colors[2]";
?>
Output: Color Name : Red Blue and Yellow
File Name : second_array.php
<?php
$colors[0]="Red";
$colors[1]="Blue";
$colors[2]="Yellow";
echo "Color Name : $colors[0], $colors[1] and $colors[2]";
?>
Output: Color Name : Red Blue and Yellow
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 Name : third_array.php
<?php
$colors = array("Red", "Blue", "Yellow");
foreach( $colors as $color )
{
echo "Color is: $color <br />";
}
?>
Output:
Color is: Red
Color is: Blue
Color is: Yellow
PHP provides count() function which returns length of an array.
<?php
$colors=array("Red","Blue","Yellow");
echo count($colors);
?>
Output: 3
Tags:PHP Arrays Numeric, Associative and Multi-Dimensional, PHP indexed array, PHP Built-in Function, Predefined Variables Examples, Object Oriented PHP, Numbers, Scalars, Arrays, foreach loop, Do, While Loops, Operators, Coding Standard, Modules, File Management, multidimensional array in PHP,PHP multidimensional associative array,PHP array add,PHP create empty array,PHP dynamic array,php array functions,php foreach array,php empty array
© 2022 Easy To Learning. All Rights Reserved | Design by Easy To Learning