Google Plus Facebook Twitter RSS YouTube

PHP Numeric Array

An array that stores values in numeric integer index system is called numeric array. In PHP programming language, numeric array accepts set of values and stores each accepted value in a numeric based place in it. The allocated place for every value in array is called key. Each key is named by numeric integer index system. In other words, the name of key in numeric array is 0, 1, 2, 3, 4, 5, ...... It can store characters, words, group of words, strings, variables, numbers and any data as values by assigning a number to each value.

php numeric array simulator

The numeric array is simulated as displayed in above graphic designing art work. Consider, there are n+1 values and each value is denoted by a ball in this graphical representation. Each value is located in a place which is known key. Each key place is denoted by numeric integer system numbers. Every key place is represented by a box in our graphic designing art work. As you know, we have assumed n+1 values so, numeric array has to place them in n key places. In this way, the concept of numeric array is defined in php and our graphic design art work helps you to understand numeric array easily.

Syntax

The syntax of numeric array is defined in PHP programming language as given in below code box.

PHP Code

array("value1","value2","value3","value4","value5","value6","value7"............);
  1. Type array word inside the PHP start and end tags.
  2. Type Parenthesis ( ) start and close brackets.
  3. Inside the open and close parenthesis, each value is placed inside either single or double quotes but each value must be separated by a comma.
  4. After parenthesis close bracket, give a semi colon to end the syntax properly.

In this way, the values in numeric array are placed in syntax.

Principle Program

As assumed in theory of numeric array simulator, consider n+1 values and each value is placed inside the array in quotes but comma separation. Also assume the entire numeric array is equal to $nar in order to use the entire array by $nar everywhere in PHP program.

PHP Code

<?php

  $nar = array("value1","value2","value3","value4","value5",............,"valuen+1");
  print_r($nar);

?>

The print_r() function returns list of stored values in array keys in web browser as demonstrated below.

Output in Web Browser
Array
(
    [0] => value1
    [1] => value2
    [2] => value3
    [3] => value4
    [4] => value5
    [5] => value6
    .............
    .............
    .............
    [n] => valuen+1                    
)

According to above browser preview, the structure of the numeric array is displayed. The first value is placed at 0, the second value is placed at 1, and this process is continued up to last value which means the n+1 value is placed at n in array.

Example Program

For example, consider five values and placed them in a numeric array and output it.

PHP Code

<body>
<?php

  $spiderman = array("Peter Parker","Ben Parker","Aunt May","Harry","Mary Jane Watson");
  
  echo "<strong>List of Some Main Characters in Spiderman Cartoon Series</strong>";
  print_r($spiderman);

?>
</body>

The print_r() function outputs the entire array in web browser as per its integer keys.

Output in Web Browser
List of Some Main Characters in Spiderman Cartoon Series
Array
(
    [0] => Peter Parker
    [1] => Ben Parker
    [2] => Aunt May
    [3] => Harry
    [4] => Mary Jane Watson
)

As you are seeing here that the first value is placed in zero key; second, third, fourth and fifth values are placed at 1, 2, 3 and 4 positions respectively in numeric array. In this way, numeric array works in php programming language.

Follow Us
Google Plus Facebook Twitter RSS YouTube
Subscribe via Email
Save or Share This PHP Tutorial with Your Friends
Save or Share PHP Tutorial