Array

Array

in

An array is a a data type offered by most programming languages that provides indexed access into a collection of variables. Arrays usually serve as computer implementations of the mathematical concepts of the vector or matrix. They also serve as the foundational type for more complicated structures such as strings, lists, and tables.

Arrays are typically useful when a programmer needs to work with a collection of similar items, such as lines of a file, a list of words, or rows retrieved from a database. Programming languages allow individual items to be retrieved from the collection--this is called indexing into an array--while maintaining the structure of the array for further operations. In addition, a programmer can typically move through all the elements of the array one by one, called iteration, or add or delete items from the beginning or end of the array, sometimes referred to as shifting, popping, pushing, or unshifting. These operations are usually trivial in memory-managed languages such as JavaScript or PHP which provide appropriate built-in functions, but in other languages such as C or C++ the allocation or deallocation of memory may be required.

The following demonstrates some basic usage of arrays in JavaScript. JavaScript uses zero-based indexing, so to get the first element of an array, you ask for index 0.

var numbers = [4, 8, 15, 16, 23, 42];
alert(numbers[0]);        // get the 1st element: "4"
numbers[3] = 17;          // sets the 4th element, which was 16, to be 17
numbers.push(43);         // pushes 43 onto the end of the array
alert(numbers.length);    // how big is the array now? "7"
alert(numbers.shift());   // removes the first element of the array and returns it
                          // result: "4"
alert(numbers.join(' '))  // joins the elements of the array with a space
                          // result: "8 15 17 23 42 43"

The following demonstrates the same operations on an array in PHP. PHP also uses zero-based indexing.

<?php                          
$numbers = array(4, 8, 15, 16, 23, 42);
echo $numbers[0];             // get the 1st element: "4"
$numbers[3] = 17;             // sets the 4th element, which was 16, to be 17
$numbers[] = 43;              // pushes 43 onto the end of the array
                              // shorthand for array_push($numbers, 43);
echo count($numbers);         // how big is the array now? "7"
echo array_shift($numbers);   // removes the first element of the array and returns it
                              // result: "4"
echo implode(' ', $numbers);  // joins the elements of the array with a space
                              // result: "8 15 17 23 42 43"

The VoiceXML 2.1 standard actually allows for dynamic flow control based on the contents of a JavaScript array using the <foreach/> tag. Here is an example of a prompt being constructed from an array of movie titles (not a complete VXML document):

<script>
  var movies = ['The Godfather', 'High Fidelity', 'Raiders of The Lost Ark'];
</script>
<!-- ... -->
<prompt>
  Please select one of the following movies.
  <break time="500ms"/>
  <foreach item="movie" array="movies">
    <value expr="movie"/>
    <break time="500ms"/>
  </foreach>
</prompt>

Related links:

Search Glossary

Term of the Day

The disconnect element is used to disconnect a specific call leg in a VoiceXML document.