![]() ![]() This chapter shows you how to use an important programming construct: arrays. The variables used in the previous chapters were scalar variables, which store a single value. We refer to an array element by putting its index in square brackets after the array name: the code a i refers to element i of array a. An array is a variable that stores a set or sequence of values. One array can have many elements, and each element can hold a single value, such as text or numbers, or another array. An array containing other arrays is known as a multidimensional array. PHP supports both numerically indexed and associative arrays. You are probably familiar with numerically indexed arrays if you've used any programming language, but unless you use PHP or Perl, you might not have seen associative arrays before. Associative arrays allow you to use more useful values as the index. Rather than each element having a numeric index, it can have words or other meaningful information. In this chapter, you continue developing the Bob's Auto Parts example using arrays to work more easily with repetitive information such as customer orders. You learned about scalar variables in Chapter 1, "PHP CrashĬourse." A scalar variable is a named location in which to store a value Key topics covered in this chapter include Likewise, you write shorter, tidier code to do some of the things you did with files in the preceding chapter. Similarly, an array is a named place to store a set of values, therebyīob's product list is the array for the example used in this chapter. Inįigure 3.1, you can see a list of three products stored in an array format. ![]() (We describe how to create a variable like this shortly.)ģ.1 Bob's products can be stored in an array.Īfter you have the information as an array, you can do a number of useful These three products are stored in a single variable called $products. Using the looping constructs from Chapter 1, you can save workīy performing the same actions on each value in the array. Information can be moved around as a single unit. Of code, all the values in the array can be passed to a function. You might want to sort the products alphabetically. Pass the entire array to PHP's sort() function. Arrays in most programming languages have EachĪrray element has an associated index (also called a key) that is The values stored in an array are called the array elements. ![]() Numerical indices that typically start from zero or one. PHP allows you to use numbers or strings as the array indices. You can useĪrrays in the traditional numerically indexed way or set the keys to be whatever We begin by looking at numerically indexed arrays and then move on to using Languages.) The programming approach may vary a little depending on whether youĪre using standard numerically indexed arrays or more interesting index (This approach may beįamiliar to you if you have used associative arrays or maps in other programming You like to make the indexing more meaningful and useful. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |