php resource centre

  • about
  • articles
  • tutorials
  • resources
  • certification
Home

Primary links

  • About
  • Articles
  • Tutorials
  • Resources
  • Certification

PHP Array Variables

admin — Wed, 30/08/2006 - 9:10pm

Array Variables

Arrays are special types that allow variables to store as many values as you want in the same variable. PHP supports numerically indexed arrays and associative arrays. An array in PHP is actually an ordered map. A map is a type that maps values to keys. Array variables consist of two parts - an index and an element. The array index, sometimes referred to as the array key, is a value used to identify or access array elements.



{mosgoogle}
The array index is placed in square brackets. The PHP server-side scripting language indexes all the values within an array using a number or a string, so you will know which of the values you’re using. Both types of arrays are created using the array() construct.

Numerically Indexed Arrays

$my_array = array('red', 'green', 'blue')

This code creates a numerically indexed array called $my_array. The array is assigned three elements - red, green, and blue. Each element is identified by a numeric index. PHP starts indexing elements numerically, from zero, and increments the element’s index with each new addition, so keep in mind that the index of the last elements in a numerically indexed array is always the total number of elements minus one.

$my_array[0] = 'red' //index 0 corresponds to element red
$my_array[1] = 'green' // index 1 corresponds to element green
$my_array[2] = 'blue' // index 2 corresponds to element blue

To access the contents of an array, use the array name and index. The following code is used to display the values of the $my_array variable.

<?php
$my_array = array('red', 'green', 'blue');
echo "The first value of the array is " . $my_array[0];
echo "The second value of the array is " . $my_array[1];
echo "The third value of the array is " . $my_array[2];
?>

 

The first value of the array is red The first value of the array is greenThe first value of the array is blue

 

Associative Arrays

Associative arrays allow you to use more useful index values. With numerically indexed arrays, index values are created automatically beginning with 0. Associative arrays permit numeric and string index values. The symbol between the index and values (=>) is an equal sign immediately followed by a greater than symbol.

 

$members = array('FName' => John, 'LName' => Smith, 'Age' => 50)

In this example, the array members contain three elements, however the string indices - FName, LName, and Age are used.

$members['FName'] = 'John' //index FName corresponds to element John
$members['LName'] = 'Smith' // index LName corresponds to element Smith
$members['Age'] = '50' // index Age corresponds to element 50

To access the contents of an array, use the array name and index. The following code is used to display the values of the $members variable.

<?php
$members = array('FName' => John, 'LName' => Smith, 'Age' => 50);
echo "The user's first name is: " . $members['FName'];
echo "The user's last name is " . $members['LName'];
echo "The user's age is " . $members['Age'];
?>

The user's first name is John
The user's last name is Smith
The user's age is 50

Array Functions

Besides the array() function, PHP includes numerous other functions for use with arrays. The following section describes some of the most common functions. A more extensive list is available at the PHP web site.

count() - the count function is used to count the number of elements in an array.

sort() - the sort function is used to sort the elements of an existing array.

shuffle() - the shuffle function is used to randomize the elements of a given array.

sizeof() - the sizeof function is an alias of the count() function.

array_slice($array_name,offset, length) - the array_slice function is used to extract a chuck of an existing array.$array_name is the name of the array to slice, offset denotes the position where the slice will begin, length indicates the number of elements that will be sliced from the array.

array_merge($array_name, $array_name) - the array_merge function is used to combine or merge two or more existing arrays. The names of the arrays are separated by commas.

PHP also includes a number of pre-defined or global arrays. These are also known as super-global variables because they are always present and available to all PHP script blocks. Following are commonly used PHP superglobal variables:

$_GET[]
$_POST[]
$_REQUEST[]
$_COOKIE[]
$_FILES[]
$_SERVER[]
$_ENV[]
$_SESSION[]

The PHP superglobal variables will be described in later tutorials. Arrays have many uses in PHP and programming in general. This section has introduced some of the basic PHP array topics and described some basic functions. This foundation will be necessary as more advanced array topics are introduced in later sections.

  • Tutorials
  • Login to post comments

User login

  • Request new password

Follow Us

Who's online

There are currently 0 users and 1 guest online.

Who's new

  • Nisha
  • linnaeus
  • Yameen
  • TalleyReedy
  • admin

Follow vipin7873 on Twitter

<!-- Start of Woopra Code -->

<!-- End of Woopra Code -->

  • about
  • articles
  • tutorials
  • resources
  • certification

copyright © 2010 Vipin Chandran