PHP Indexed Array

Index array is an array with a numeric key. The array index start from zero. There are two ways to create indexed arrays. The first one is by using the array() function, with which we can automatically assigned the values on it. For example :
 $array_name = array(value1, value2, value3, ...., valueN);
Example :
<?php
  $WebDev = array("HTML", "CSS", "JavaScript", "PHP", "MySql");
  echo "The Web Devlopment Languages are<br/><br/>";
  echo "Front End : " . $WebDev[0] . ", " . $WebDev[1] . ", " . $WebDev[2] . "<br/>";
  echo "Back End : " . $WebDev[3] . ", " . $WebDev[4];
?>
Output :

The Web Devlopment Languages are

Front End : HTML, CSS, JavaScript
Back End : PHP, MySql

Or we can also assign the values manually like :
  $array_nam[0] = value1;
  $array_nam[1] = value2;
  $array_nam[2] = value3;
  $array_nam[N-1] = valueN;
Example :
<?php
  $WebDev[0] = "HTML";
  $WebDev[1] = "CSS";
  $WebDev[2] = "JavaScript";
  $WebDev[3] = "PHP";
  $WebDev[4] = "MySql";

  echo "The Web Devlopment Languages are<br/><br/>";
  echo "Front End : " . $WebDev[0] . ", " . $WebDev[1] . ", " . $WebDev[2] . "<br/>";
  echo "Back End : " . $WebDev[3] . ", " . $WebDev[4];
?>
Output :

The Web Devlopment Languages are

Front End : HTML, CSS, JavaScript
Back End : PHP, MySql


The count() function :

The count() function is used to return the length or the number of elements of an array. For example :
<?php
  $WebDev = array("HTML", "CSS", "JavaScript", "PHP", "MySql");
  echo count($WebDev);
?>
Output :

5


Loop Through the Indexed Array :

By using the for loop we can get all the values of an index array. For example :
<?php
  $WebDev = array("HTML", "CSS", "JavaScript", "PHP", "MySql");
  $length = count($WebDev);

  for($x = 0; $x < $length; $x++) {
    echo $WebDev[$x] . "<br/>";
  }
?>
Output :

HTML
CSS
JavaScript
PHP
MySql


Next Topic :