PHP sorts array values and uses another container. php array sorting _ PHP Tutorial

Source: Internet
Author: User
Tags array sort
PHP sorts the array values by using another container and php array. PHP sorts the array values and uses another container. php sorts the array values and uses another container? Php * sorting method: in fact, you only need to sort the array values cyclically in PHP, and use another container to sort the php array.
PHP sorts the array values and uses another container
 $ 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. enter the student ID, name, and score columns in the form. there are 5 rows in total.

It took a while to finish this job. The following is the code. although the efficiency is low, why not use a database?


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:
";
Echo "";

// 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'
';
print_r($stu);
echo '
';

}

// Sort
$ Temp = sysSortArray ($ stu, 'price', "SORT_ASC ");

Echo'
';
print_r($temp);
echo '
'

?>

How to sort key-value arrays in php

Array_change_key_case -- returns an array whose string key names are all 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

Http://www.bkjia.com/PHPjc/827774.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/827774.htmlTechArticlePHP to sort the array values, use another container, php array sort PHP to sort the array values, use another container? Php/* sorting method: in fact, you only need to loop...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.