PHP and MySQL Web Development - P23

PHP and MySQL Web Development - P23: PHP and MySQL Web Development teaches the reader to develop dynamic, secure, commercial Web sites. Using the same accessible, popular teaching style of the first edition, this best-selling book has been updated to reflect the rapidly changing landscape of MySQL and PHP. | Multidimensional Arrays 77 Multidimensional Arrays Arrays do not have to be a simple list of keys and values each location in the array can hold another array. This way we can create a two-dimensional array. You can think of a two dimensional array as a matrix or grid with width and height or rows and columns. If we want to store more than one piece of data about each of Bob s products we could use a two-dimensional array. Figure shows Bob s products represented as a two-dimensional array with each row representing an individual product and each column representing a stored product attribute. Code Description Price TIR Tires 100 OIL Oil 10 SPK Spark Plugs 4 product attribute Figure We can store more information about Bob s products in a two- dimensional array. Using PHP we would write the following code to set up the data in the array shown in Figure . products array array TIR array OIL Tires 100 Oil 10 Spark Plugs 4 array SPK You can see from this definition that our products array now contains three arrays. To access the data in a one-dimensional array recall that we need the name of the array and the index of the element. A two-dimensional array is similar except that each element has two indices a row and a column. The top row is row 0 and the far left column is column 0. To display the contents of this array we could manually access each element in order like this echo . products 0 0 . . products 0 1 . . products 0 2 . br echo . products 1 0 . . products 1 1 . . products 1 2 . br echo . products 2 0 . . products 2 1 . . products 2 2 . br 78 Chapter 3 Using Arrays Alternatively we could place a for loop inside another for loop to achieve the same result. for row 0 row 3 row for column 0 column 3 column echo . products row column echo br Both versions of this code produce the same output in the browser TIR Tires 100 OIL Oil 10 SPK Spark Plugs 4 The only difference between the two examples is that your code will be shorter if you use the second version

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.