PHP Deduction (2) - Arrangement Sort

zhaozj2021-02-16  47

Sort in the second array

The sort of arrays is a problem that people who are very headache. The exquisite method of various names will make people halo, but it is important, so that we can't focus on it. Fortunately, PHP has provided a lot of sorted functions, so you can not understand the details of these sort. Comparison of principles and efficiency of sorting, we will be discussed in the chapters of the rear.

The most general sort function in PHP is sort (), and the PHP manual is described below:

Sort (PHP 3, PHP 4) Sort - Randle the array Description Void Sort (Array Array [, int Sort_Flags] This function is sorted to the array. When the end of this function, the array unit will be rearranged from the lowest to the highest.

Using sort () will rearrange the original array according to the ASCII code from small to large order, note that sort () is a direct pair of array and does not return any values. Now let's go back to mahjong :)), MajiangArray's array element is like this: color color _ points. GIF, we need to sort this: First, according to the color color, the MaijiangaRray array, and each color is inside, according to points Sort in size. Obviously, the Sort () function on the upper side is in line with this requirement. We only need to be ok before ShoWarray.

PHP Code

// Sort () sort ($ USER2); Sort ($ USER2); Sort ($ USER3); Sort ($ USER1); Showarray ($ USER2); ShowArray ($ USER2); SHOWArray ($ user2); ShowArray ($ USER4);

The effect chart of the program run is as follows:

The example of the upper exemption can be solved because we pre-organized the content of the elements in advance (), and put the main sort standard - color color in front, and the secondary sorting standard - the number of points is placed behind, and Split them is the same symbol "_". If there is an element 10_10.gif, then the existing order will be disrupted; and the method of reinaged the name of the restructuring is always uncomfortable. This indicates that simple sorting is actually not practical because we are usually sorted in accordance with a feature of array elements. In practical applications, we store array elements with multiple properties through multi-dimensional arrays. For example, an example, we use the user_info to store the accesser's information, visitors ID, visitors' IP, access date, and time, language, and reference pages are saved in user_info ["ID"], user_info [IP "], user_info [" date "], user_info [" time "], user_info [" language "] and user_info [" Reference "]. If we need to sort the USER_ INFO array according to the access time, sort () is useless to the hero. Fortunately, the PHP provides a function array_multisort () for multi-dimensional array sorting, which can be sorted according to one property of a multi-dimensional array of properties. Let's take a look at its role.

PHP Code

Sequence number IP date time Reference Page '; for ($ I = 0; $ i '. $ Berray ["id"] [$ i ]. ' '. $ USERARRAY ["IP"] [$ I]. ' '. $ USERARRAY [" Date "] [$ I]. ' '. $ uSERARRAY [" Time "] [$ I]. ' < TD width = "14%"> '. $ uSERARRAY ["Language"] [$ I].' '. $ uSERARRAY ["

Reference "] [$ I]. ' ';} echo '';} unset ($ user_info); // Start storage user information // First $ user_info [" ID "] [] = 1; $ USER_INFO [" ip "] [] = '202.204.203.7'; $ user_info [" date "] [] = '06 -07-2002 '; $ user_info [" time "] [ ] = '20: 24 '; $ USER_INFO ["Language"] [] =' zh-cn '; $ USER_INFO ["Reference"] [] =' http://exlc.soft.com/es5 '; // Second $ user_info ["ID"] [] = 2; $ USER_INFO ["IP"] [] = '202.204.203.8'; $ user_info ["date"] [] = '06 -08-2002 '; $ User_info ["time"] [] = '04: 32 '; $ user_info ["language"] [] =' zh-cn '; $ user_info ["refrence"] [] =' http: //exlc.soft. COM / ES5 '; // Third $ USER_INFO ["ID"] [] = 3; $ USER_INFO [IP "] [] =' 202.204.203.9 '; $ user_info [" Date "] [] = '06 -09-2002 '; $ USER_INFO ["Time"] [] = '05: 12'; $ user_info ["izguage"] [] = 'zh-cn'; $ user_info ["reason"] [] = 'http : //exlc.soft.com/es5 '; // Sort array_multisort to the array at time to the index ($ user_info ["time"], sort_string, sort_asc, $ user_info ["id"], // need to remain associated with time The attribute item is written behind $ USER_INFO ["IP"], $ USER_INFO ["Date"], $ USER_INFO ["Language"], $ USER_INFO ["Referen CE "]); showuser ($ user_info); // Look at the results?> Sorted results are as follows:

The multidimensional array is an important means of implementing the PHP text database. In the next section we will try to write a class to encapsulate the operation of the text data sheet, of course, the Class inside the Class uses a multi-dimensional array to be implemented.

转载请注明原文地址:https://www.9cbs.com/read-25660.html

New Post(0)