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

How To

How to Create an Array in PHP

Contributor
By eHow Contributing Writer
(7 Ratings)

Arrays are data structures, and they are frequently used in all programming languages, including PHP. In PHP, an array is actually a map, because each key is mapped to a value. Arrays are very useful for storing data in cases when a variable can have many values.

Difficulty: Moderately Easy
Instructions

Things You'll Need:

  • PHP 5, installed and properly configured
  • PHP IDE
  • Web server (preferably Apache)
  • MySQL database server, configured for work with PHP

    Analyze the Data That Will Go Into the Array

  1. Step 1

    Imagine that you have to write a movie catalog. One of the variables that you will use in your program is the movie title. But if you have thousands of movies, using a separate variable to store each title is not ideal. Instead, you should use one variable (the title) which has many values ("One Flew Over the Cuckoo's Nest," "The Graduate" and so on). Such data is an ideal candidate for an array.

  2. Step 2

    Check to see if you already have a list of values, so that you can create the array with the array function instead of populating it manually.

  3. Create the Array

  4. Step 1

    Declare the array and assign values:
    $titles = array("Hair", "The Office", "Troy", "Tarzan", "American Pie", "Adam and Eve", "Mystery", "E.T.", "Star Wars");
    Enter as many movie titles as you have. If your values are strings, as in the above example, don't forget the quotes around them. If your values are integers, you can forgo the quotes.

  5. Step 2

    Appreciate that this array is created with numeric indexing. In the above example, the array has nine elements (movie titles) and the indexes are from 0 ("Hair") to 8 ("Star Wars"). However, you can also create associative arrays.

  6. Step 3

    Create an associative array. An associative array uses textual keys instead of numbers, and the indexes are more descriptive. This is especially useful when the values are not strings. The general syntax is the following:
    $salary["John Smith"] = 3000;
    This will assign the value 3000 to the array element, which has the "John Smith" index.

  7. Step 4

    Use the array function to create the array.
    $salary = array("John Smith" => 3000, "Sally Jones" => 4000, "Chris Steward" => 4900, "Mary Roberts" => 6500, "Sam Moses" => 5400, "Alice Roberts" => 4200);
    Notice the slight difference in the syntax: You use the => symbol to enter the value for the key.

  8. Perform Simple Operations With the Array

  9. Step 1

    Reference values from the array by their index. For instance, if you want to display the title "Adam and Eve," you would do the following:
    echo $titles[5];
    because "Adam and Eve" is the sixth element in the array and its index is 5.

  10. Step 2

    Assign values to array elements. If you want to set a new value for an array element, use the following:
    $titles[6] = "Midnight Express";
    This will replace the "Mystery" value with "Midnight Express".

Tips & Warnings
  • You can also use the array function to quickly build an array from a supplied list of values, rather than add the elements one by one.
  • Numeric arrays start from zero. In other words, the first member of an array has an index of 0, the second has an index of 1 and so on. You can define a 1-based index array, but it is a more advanced topic.
  • If you start using non-zero based arrays, make sure that you are consistent in your code. All your arrays should start the same way, from 1 or from 0.
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Tags
Get Free Internet 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. † requires javascript

Demand Media
eHow_eHow Technology and Electronics