TAILIEUCHUNG - Professional Information Technology-Programming Book part 70

Tham khảo tài liệu 'professional information technology-programming book part 70', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Array Functions You have already seen the array function used to generate an array from a list of values. Now let s take a look at some of the other functions PHP provides for manipulating arrays. There are many more array functions in PHP than this book can cover. If you need to perform a complex array operation that you have not learned about refer to the online documentation at . Sorting To sort the values in an array you use the sort function or one of its derivatives as in the following example sort temps Sorting Functions sort and other related functions take a single array argument and sort that array. The sorted array is not returned the return value indicates success or failure. Sorting the original temps array with sort arranges the values into numeric order but the key values are also renumbered. After you perform the sort index 0 of the array will contain the lowest value from the array and there is no way of telling which value corresponds to each month. You can use asort to sort an array while maintaining the key associations whether it is an associative array or numerically indexed. After you sort temps index 0 will still contain January s average temperature but if you loop through the array the elements will be retrieved in sorted order. Using the associative array temps as an example the following code displays the months and their average temperatures from coldest to hottest temps array jan 38 feb 40 mar 49 apr 60 may 70 jun 79 jul 84 aug 83 sep 76 oct 65 nov 54 dec 42 asort temps foreach temps as month temp print month temp br n It is also possible to sort an array on the keys rather than on the element values by using ksort. Using ksort on the associative temps array arranges the elements alphabetically on the month name keys. Therefore when you loop through the sorted array the first value fetched would be temps apr followed by temps aug and so on. To reverse the sort order for any of these functions you use rsort in place

Đã 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.