PHP sorts array values and uses another container to sort php arrays.
PHP sorts the array values and uses another container
<? Php/* sorting method: in fact, you only need to loop the array to N times, and then take the maximum value */$ array = array (10,258, 333,108, 1.2, 0.7, 33,); // 3 1 2 | 1 2 | 1 | $ array2 = array (); for ($ y = 1; $ y <= 17; $ y ++) {$ max = 0; for ($ I = 0; $ I <count ($ array); $ I ++) {if ($ array [$ I]> $ max) {$ index = $ I; $ max = $ array [$ I] ;}} unset ($ array [$ index]); sort ($ array); array_push ($ array2, $ max);} print_r ($ array2);?>
1. Use the sorting function of PHP to sort the following arrays. 2. in the form, the user enters the student ID, name, and score data in these three columns. A total of five rows are charged for a period of time, it is also a kind of exercise to finish this job. The following is the code. Although the efficiency is relatively low, why not use a database?
<? Php
Header ('content-type: text/html; charset = UTF-8 ');
// Sort two-dimensional arrays
Function sysSortArray ($ ArrayData, $ KeyName1, $ SortOrder1 = "SORT_ASC", $ SortType1 = "SORT_REGULAR ")
{
If (! Is_array ($ ArrayData )){
Return $ ArrayData;
}
// Get args number.
$ ArgCount = func_num_args ();
// Get keys to sort by and put them to SortRule array.
For ($ I = 1; $ I <$ ArgCount; $ I ++ ){
$ Arg = func_get_arg ($ I );
If (! Eregi ("SORT", $ Arg )){
$ KeyNameList [] = $ Arg;
$ SortRule [] = '$'. $ Arg;
} Else {
$ SortRule [] = $ Arg;
}
}
// Get the values according to the keys and put them to array.
Foreach ($ ArrayData as $ Key => $ Info ){
Foreach ($ KeyNameList as $ KeyName ){
$ {$ KeyName} [$ Key] = $ Info [$ KeyName];
}
}
// Create the eval string and eval it.
$ EvalString = 'array _ multisort ('. join (",", $ SortRule).', $ ArrayData );';
Eval ($ EvalString );
Return $ ArrayData;
}
$ Keys = array ('stu _ no', 'name', 'price ');
// Output table
Echo "Enter the data to be sorted: <br> ";
Echo "<form method = post> ";
Echo "<table> ";
Echo "<tr> <td> Student ID </td> <td> name </td> <td> score <td> </tr> ";
For ($ row = 1; $ row <6; $ row ++ ){
Echo "<tr> ";
For ($ col = 1; $ col <4; $ col ++ ){
Echo "<td> <input type = 'text' name = 'stu _ $ row". "_ $ col 'size = '10'> </td> ";
}
Echo "</tr> ";
}
Echo "</table> ";
Echo "<input type = 'submit 'name = 'bt' value = 'Submit'> ";
Echo "</form> ";
// Convert the Array
If (isset ($ _ POST ['bt ']) {
For ($ row = 1; $ row <6; $ row ++)
For ($ col = 1; $ col <4; $ col ++ ){
$ Key = $ keys [$ col-1];
$ Stu [$ row] [$ key] =_ _ POST ['stu _ '. $ row.' _ '. $ col];
}
Echo '<pre> ';
Print_r ($ stu );
Echo '</pre> ';
}
// Sort
$ Temp = sysSortArray ($ stu, 'price', "SORT_ASC ");
Echo '<pre> ';
Print_r ($ temp );
Echo '</pre>'
?>
How to sort the key-value array array_change_key_case in php -- returns an array with all string key names in lowercase or uppercase.
Array_chunk -- splits an array into multiple
Array_combine -- creates an array with the value of one array as its key name and the value of another array as its value
Array_count_values -- count the number of occurrences of all values in the array
Array_diff_assoc -- calculate the array's difference set with index check
Array_diff_uassoc -- Computes the difference of arrays with additional index check which is specified med by a user supplied callback function.
Array_diff -- calculate the difference set of the array
Array_fill -- fill the array with the given value
Array_filter -- use the callback function to filter elements in the array.
Array_flip -- exchange keys and values in an array
Array_intersect_assoc -- intersection of arrays computed by checking Indexes
Array_intersect -- calculates the intersection of Arrays
Array_key_exists -- check whether the given key name or index exists in the array
Array_keys -- returns all the key names in the array.
Array_map -- calls the callback function to the unit of the given array.
Array_merge_recursive -- recursively merges two or more Arrays
Array_merge -- merge two or more Arrays
Array_multisort -- sorts multiple arrays or multi-dimensional arrays.
Array_pad -- fill the array with a value to the specified length
Array_pop -- bring up the last unit of the array (output stack)
Array_push -- push one or more units to the end of the array (into the stack)
Array_rand -- randomly retrieve one or more units from the array
Array_reduce -- use the callback function to iteratively simplify the array to a single value
Array_reverse -- returns an array with the opposite unit order.
Array_search -- search for a given value in the array. If the value is successful, the corresponding key name is returned.
Array_shift -- removes elements starting with an array from the array
Array_slice -- extract a segment from the array
Array_splice -- remove part of the array and replace it with other values
Array_sum -- calculate the sum of all values in the array
Array_udiff_assoc -- Computes the difference of arrays with additional index check. The data is compared by using a callback function.
Array_udiff_uassoc -- Computes the difference of arrays with additional index check. The data is compared by using a callback function. The index check is done by a callback function also
Array_udiff -- Computes the difference of arrays by using a callback function for data comparison.
Array_unique -- remove repeated values from the array
Array_unshift -- insert one or more units at the beginning of the array
Array_values -- returns all values in the array.
Array_walk -- apply user functions to each member in the array
Array -- create an array
Arsort-sorts arrays in reverse order and maintains the index relationship.
Asort -- sorts arrays and maintains the index relationship.
Compact -- create an array, including variable names and their values
Count -- count the number of units in the Variable
Current -- returns the current unit in the array.
Each -- returns the current key/value pair in the array and moves the array pointer one step forward.
End -- points the internal pointer of the array to the last unit.
Extract -- import the variable from the array to the current symbol table
In_array -- check whether a value exists in the array
Key -- get the key name from the combined Array
Krsort-sort the array in reverse order by key name
Ksort -- Sort the array by key name
List -- assign values in the array to some variables
Natcasesort-use the "natural sorting" algorithm to sort arrays by case-insensitive letters
Natsort -- Sort arrays using the "natural sorting" Algorithm
Next -- move the internal pointer in the array to a forward position
Pos -- get the current Unit of the array
Prev -- returns the internal pointer of the array to one
Range -- create an array containing the specified range units
Reset -- point the internal pointer of the array to the first unit
Rsort -- reverse sorting of Arrays
Shuffle -- disrupt the Array
Sizeof -- count () alias
Sort -- sort Arrays
Uasort -- Sort the values in the array using the User-Defined comparison function and maintain the index Association
Uksort -- Sort the key names in the array using the User-Defined comparison Function
Usort -- Sort the values in the array using the User-Defined comparison Function