Chapter 6. Arrays!

  • Initializing Arrays

  • Using Strings for Indexes

  • Looping through Sequential Arrays

  • Looping through Non-Sequential Arrays

  • Multi-Dimensional Arrays

  • Sorting Arrays

  • Your First PHP Game

In Chapter 4, when you were making the form processing example, the check boxes were named nEquipmentID[]. Those two brackets mean that it is an array. An array consists of several elements, each of which has a value. You can access each element in the array by using an index. In PHP your index can be either an integer or a string, which allows a lot of flexibility in your code.

NOTE

As in C/C++, arrays in PHP are zero based, meaning that the first index in the array starts at 0.

Take a look at Figure 6.1. This representation should make it clear how an array stores its data.

Figure 6.1. How an array stores data.


In this case the array is named $board, and it has four elements. The first element of the array, $board[0], is equal to “A”. The second element of the array, $board[1], is equal to “B,” and so on. So how do you create arrays?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset