Core PHP

Basic Array Usage

An array holds a list of data items sequentially. Typically, these items are selected and accessed via a numeric index, as shown below

BasicArrayUsage.php

<?php
  // Create an empty array
  $saSonsOfIsaac = array();

  // Push entries into the array
  array_push($saSonsOfIsaac, "Esau");
  array_push($saSonsOfIsaac, "Jacob");

  // Get the size of the array
  echo "<b>Get the size of the array</b><br />";
  echo "The array has ".count($saSonsOfIsaac)." entries.<br /><br />";

  // Individual element access
  echo "<b>Access individual elements</b><br />";
  echo "Entry 0: ".$saSonsOfIsaac[0]."<br />";
  echo "Entry 1: ".$saSonsOfIsaac[1]."<br />";
  echo "<br />";

  // Use a for loop to print each entry via the index
  echo "<b>For loop with Index Access:</b><br />";
  $iSize = count($saSonsOfIsaac);
  for ($i = 0; $i < $iSize; ++$i) {
    // Braces signify the value of the variable
    echo "{$saSonsOfIsaac[$i]}<br />";
  }
?>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.