close[x]


PHP

PHP-Home PHP-Environment Setup PHP-Syntax PHP-Run PHP in XAMPP PHP-Variable PHP-Comment PHP-Datatype PHP-String PHP-Operators PHP-Decision PHP-loop PHP-Get/Post PHP-Do While loop PHP-While loop PHP-For loop PHP-Foreach loop PHP-Array PHP-Multidimensional Arrays PHP-Associative Arrays PHP-Indexed Arrays PHP-Function PHP-Cookies. PHP-Session PHP-File upload PHP-Email PHP-Data & Time PHP-Include & Require PHP-Error PHP-File I/O PHP-Read File PHP-Write File PHP-Append & Delete File PHP-Filter PHP-Form Validation PHP-MySQl PHP-XML PHP-AJAX



learncodehere.com



PHP - Numeric (Indexed) Array

These arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero.

There are two ways to create indexed arrays:

Syntax : Create Numeric Array


 //index can be assigned automatically (index always starts at 0)
 $variable = array("value1", "value2",.... "valueN");
//the index can be assigned manually
$variable[0] = "value1";
$variable[1] = "value2";
$variable[2] = "value3";
.
.
$variable[N] = "valueN";

Let's put these syntax into real use.

Following is the example showing how to create and access numeric arrays.

Example : foreach Loop


<?php 
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $value ) {
   echo "Value is $value <br />";
}
/* Second method to create array. */
$numbers[0] = "one";
$numbers[1] = "two";
$numbers[2] = "three";
$numbers[3] = "four";
$numbers[4] = "five";
foreach( $numbers as $value ) {
   echo "Value is $value <br />";
}
?>

This will produce the following result −

Result


Value is 1 
Value is 2 
Value is 3 
Value is 4 
Value is 5 
Value is one 
Value is two 
Value is three 
Value is four 
Value is five