How to use numeric array in PHP

In this article, I will explain how the numeric array can be used in PHP.
  • 2269

Array in PHP

  • An array is a data structure that stores one or more similar type of values in a single value.
  • Each element in the array has its own index so that it can be easily accessed.
  • An array in PHP is a collection of key/value pairs.

There are three different kind of arrays and each array value is accessed using an ID c which is called array index.

  • Numeric array - An array with a numeric index. Values are stored and accessed in linear fashion
  • Associative array - An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
  • Multidimensional array - An array containing one or more arrays and values are accessed using multiple indices

Numeric Array

  • Numeric array stores each array element with a numeric index.

Example

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

<html>

<body>

<h2 style="color: red;">Numeric Array example</h2>

    <?php

    /* First method to create array. */

    $num = array( 1, 2, 3, 4, 5);

    foreach( $num as $value )

    {

    echo "Array Value is $value <br />";

    }

    /* Array create with key */

    $num[0] = "one";

    $num[1] = "two";

    $num[2] = "three";

    $num[3] = "four";

    $num[4] = "five";

    foreach( $num as $value )

    {

    echo "Array Value is $value <br />";

    }

    ?>

</body>

</html>

 

Output

NUMERIC ARRAY.jpg

You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 
© 2020 DotNetHeaven. All rights reserved.