tailieunhanh - Phát triển web với PHP và MySQL - p 12

Using Arrays CHAPTER 3 85 Alternatively, we can use the array_reverse() function to reverse the array created by range(). $numbers = range(1,10); $numbers = array_reverse($numbers); Note that array_reverse() returns a modified copy of the array. Because we did not want the original array, we simply stored the new copy over the original. Loading Arrays from Files In Chapter 2, “Storing and Retrieving Data,” we stored customer orders in a file. Each line in the file looks something like 15:42, 20th April 4 tires 1 oil 6 spark plugs $ 22 Short St, Smalltown To process or fulfill this order, we could load it back into an array. Listing. | Using Arrays 85 Chapter 3 Alternatively we can use the array_reverse function to reverse the array created by range . numbers range 1 10 numbers array_reverse numbers Note that array_reverse returns a modified copy of the array. Because we did not want the original array we simply stored the new copy over the original. Loading Arrays from Files In Chapter 2 Storing and Retrieving Data we stored customer orders in a file. Each line in the file looks something like 15 42 20th April 4 tires 1 oil 6 spark plugs 22 Short St Smalltown To process or fulfill this order we could load it back into an array. Listing displays the current order file. Listing Using PHP to Display Orders for Bob orders file . . orders number_of_orders count orders if number_of_orders 0 echo p strong No orders pending. Please try again later. strong p for i 0 i number_of_orders i echo orders i . br This script produces almost exactly the same output as Listing in the previous chapter which is shown in Figure . This time we are using the function file which loads the entire file into an array. Each line in the file becomes one element of an array. This code also uses the count function to see how many elements are in an array. Furthermore we could load each section of the order lines into separate array elements to process the sections separately or to format them more attractively. Listing does exactly that. 3 Using Arrays Using PHP 86 Part I Listing Using PHP to Separate Format and Display Orders for Bob html head title Bob s Auto Parts - Customer Orders title head body h1 Bob s Auto Parts h1 h2 Customer Orders h2 Read in the entire file. Each order becomes an element in the array orders file . . orders count the number of orders in the array number_of_orders count orders if number_of_orders 0 echo p strong No orders pending. Please try again later. strong p echo table border 1 n echo tr th bgcolor CCCCFF Order .

TỪ KHÓA LIÊN QUAN