Home > Backend Development > PHP Tutorial > PHP array functions array_map, array_multisort multidimensional array sorting examples

PHP array functions array_map, array_multisort multidimensional array sorting examples

WBOY
Release: 2016-07-25 09:04:05
Original
1252 people have browsed it
  1. array_sort($arrFile, 1, 1);//Sort according to the name field
  2. array_sort($arrFile, 3, 1);//Sort according to the size field
  3. / *
  4. @records Array to be sorted
  5. @field Field to be sorted, pay attention to whether it is a number
  6. @reverse positive or negative order
  7. */
  8. function _array_sort($records, $field, $reverse, $defaultSortField = 0)
  9. {
  10. $uniqueSortId = 0;
  11. $hash = array();
  12. $sortedRecords = array();
  13. $tempArr = array();
  14. $indexedArray = array();
  15. $recordArray = array();
  16. foreach($records as $record)

  17. {
  18. $uniqueSortId++;
  19. $recordStr = implode("|", $record)."|".$uniqueSortId;
  20. $recordArray[] = explode(" |", $recordStr);
  21. }

  22. $primarySortIndex = count($record);

  23. $records = $recordArray;

  24. foreach($records as $record)

  25. {
  26. $hash[$record[$primarySortIndex]] = $record[$field];
  27. }
  28. uasort($hash, "strnatcasecmp");
  29. if($reverse)
  30. $hash = array_reverse($ hash, true);

  31. $valueCount = array_count_values($hash);

  32. foreach($hash as $primaryKey => $value)

  33. {
  34. $indexedArray[] = $primaryKey;
  35. }

  36. $i = 0;

  37. foreach($hash as $primaryKey => $value)
  38. {
  39. $i++;
  40. if($valueCount [$value] > 1)
  41. {
  42. foreach($records as $record)
  43. {
  44. if($primaryKey == $record[$primarySortIndex])
  45. {
  46. $tempArr[$record[$defaultSortField]."__ ".$i] = $record;
  47. break;
  48. }
  49. }

  50. $index = array_search($primaryKey, $indexedArray);

  51. if( ($i == count($records)) || ($value != $hash[$indexedArray[$index+1]]))

  52. {
  53. uksort($tempArr, "strnatcasecmp");

  54. if($reverse)

  55. $tempArr = array_reverse($tempArr);

  56. foreach($tempArr as $newRecs)

  57. {
  58. $sortedRecords [] = $newRecs;
  59. }

  60. $tempArr = array();

  61. }
  62. }
  63. else
  64. {
  65. foreach($records as $record)
  66. {
  67. if($primaryKey == $record[$primarySortIndex] )
  68. {
  69. $sortedRecords[] = $record;
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. return $sortedRecords;
  76. }

Copy code

2. Use array_map and arra y_mutisort to sort array_mutisor can also perform secondary or triple sorting based on multiple values, which is incomparable to the previous function. Use array_map to get the array to be sorted by $arrField = array_map(create_function('$n', 'return $n["size"];'), $arrFile); //Use array_mutisort to sort $array_multisort($arrField, SORT_DESC, $arrFile);

3. Final test Test with an array of 188 data, sort 50 times and find the average. Way 1: 0.04269016 name 0.04267142 size

Method 2: 0.001249 name 0.00083924 size

>>> For more information, please view the complete list of php array sorting methods



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template