Simple php two-dimensional array multi-element sorting example

Source: Internet
Author: User
Php two-dimensional array multi-element sorting editor has introduced a lot, but this year I saw a little difference in this example, so I sorted it out for you, I hope this sorting algorithm can help you. Write the multi-element sorting of a two-dimensional array. the multi-element sorting is similar to the order... php two-dimensional array multi-element sorting editor has introduced a lot, but this year I saw a little difference in this example, so I sorted it out for you, I hope this sorting algorithm can help you.

Write a two-dimensional array of multi-element sorting. multi-element sorting is similar to order by sort and date in SQL. the following code considers performance issues and only involves two elements for sorting.

Code area:

 $ V) {$ key_count = count ($ old_arr)-1; // The number of the first element is equal, put the temporary array if ($ v [$ arr1 [0] ==$ old_arr [$ k-1] [$ arr1 [0] | empty ($ temp_array )) {$ temp_array [$ k] = $ v;} if ($ v [$ arr1 [0]! = $ Old_arr [$ k-1] [$ arr1 [0] &! Empty ($ temp_array) | $ key_count = $ k) {// add the latest element $ temp_array [$ k] = $ v; // when the array is greater than 1, sort if (count ($ temp_array)> 1) {if ($ key_count! = $ K) {// delete the last (not equal to the last element) unset ($ temp_array [$ k]);} // not the last or last, sort if ($ key_count! = $ K | $ v [$ arr1 [0] = $ old_arr [$ k-1] [$ arr1 [0]) {// sort the second element $ temp_array = array_sort ($ temp_array, $ arr2 [0], $ arr2 [1]);} // sort the sorted array, insert the original array to the position before sorting $ count = count ($ new_temp_array) = 0? 0: count ($ new_temp_array); foreach ($ temp_array as $ val_t) {$ new_temp_array [$ count] = $ val_t; $ count ++ ;} // empty the original array after writing the new array $ temp_array = array (); // add the latest element $ temp_array [$ k] = $ v ;}}return $ new_temp_array ;} // sort function array_sort ($ arr, $ keys, $ type = 'asc ') {$ keysvalue = $ new_array = array (); // Obtain the element value, generate a new array foreach ($ arr as $ k => $ v) {$ keysvalue [$ k] = $ v [$ keys];} // sort, the default value is if ($ type = 'asc ') {asort ($ keysvalue);} else {arsort ($ keysvalue);} reset ($ keysvalue ); $ count_s = 0; // restore key value foreach ($ keysvalue as $ k => $ v) {$ new_array [$ count_s] = $ arr [$ k]; $ count_s ++;} return $ new_array;} // call method: $ old_arr = array ('id' => 1, 'sort '=> 1, 'date' => '123'), array ('id' => 2, 'sort '=> 1, 'date' => '123 '), array ('id' => 3, 'sort '=> 2, 'date' => '123'),); $ arr1 = array ('sort ', 'desc'); $ arr2 = array ('date', 'desc'); $ newArr = multipleArraySort ($ old_arr, $ arr1, $ arr2); var_dump ($ newArr );

// Print the result:

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.