eHow launches Android app: Get the best of eHow on the go.

How To

How to Use the Array Command in PHP

Contributor
By eHow Contributing Writer
(5 Ratings)

The array command in PHP creates and returns an array with the listed parameters. Index values are optional and may be provided with the => operator. This command is a language construct, not a function. The following steps will aid in your search for information about this command.

Difficulty: Moderately Challenging
Instructions
  1. Step 1

    Learn the syntax. The complete syntax is:

    array ([index 1 =>] value 1, [index 2 =>] value 2, [index 2 =>] value 1[,]...).

    The brackets "[]" indicate an optional part and the ellipsis "..." indicates you may continue the pattern as needed. Note that a trailing comma is an optional but valid syntax.

  2. Step 2

    Understand how the indexes are generated. The first index will be 0 and each subsequent index will be incremented by one unless otherwise specified. The indexes and values may be strings or numeric. The last index overwrites the first index when two identical ones are specified.

  3. Step 3

    Look at the following example for a simple array:

    $myArray = array("red", "blue", "green");
    ?>

    This example creates an array called myArray with the value "red" at index 0, the value "blue" at index 1 and the value "green" at index 2.

  4. Step 4

    Look at this example of how to specify indexes:

    $myArray = array(3=>"red", 2=>"blue", 1=>"green");
    ?>

    This example creates an array called myArray with the value "red" at index 3, the value "blue" at index 2 and the value "green" at index 1.

  5. Step 5

    Look at this example of how to initialize an array:

    $myArray = array(3=>"red", "blue", "green");
    ?>

    This example creates an array called myArray with the value "red" at index 3, the value "blue" at index 4 and the value "green" at index 5. Notice how the index is initialized to 3 and each subsequent index is incremented by one.

Post a Comment

Post a Comment
  • Have you done this? Click here to let us know.
I Did This

Related Ads

Computers
Alexia Petrakos,

Meet Alexia Petrakos eHow's Computers Expert.

Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics