PageRenderTime 76ms CodeModel.GetById 25ms RepoModel.GetById 4ms app.codeStats 0ms

/vendor/Eva/Stdlib/Arraylib/Sort.php

https://github.com/zc1415926/eva-engine
PHP | 69 lines | 31 code | 7 blank | 31 comment | 3 complexity | 344fecf09a5c844791cc43143df72fd5 MD5 | raw file
  1. <?php
  2. /**
  3. * EvaEngine
  4. *
  5. * @link https://github.com/AlloVince/eva-engine
  6. * @copyright Copyright (c) 2012 AlloVince (http://avnpc.com/)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Eva_Api.php
  9. * @author AlloVince
  10. */
  11. namespace Eva\Stdlib\Arraylib;
  12. /**
  13. * Unique Hash
  14. * From : http://blog.kevburnsjr.com/php-unique-hash
  15. *
  16. * @category Eva
  17. * @package Eva_Stdlib
  18. */
  19. class Sort
  20. {
  21. /**
  22. * Sort an two-dimension array by some level two items use array_multisort() function.
  23. *
  24. * sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2")
  25. * @author Chunsheng Wang <wwccss@263.net>
  26. * @param array $arrayData the array to sort.
  27. * @param string $keyName the first item to sort by.
  28. * @param string $sortOrder the order to sort by("SORT_ASC"|"SORT_DESC")
  29. * @param string $sortType the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING")
  30. * @return array sorted array.
  31. */
  32. public static function multiSortArray($arrayData, $keyName, $sortOrder = "SORT_ASC", $sortType = "SORT_REGULAR")
  33. {
  34. if (!is_array($arrayData)) {
  35. return $arrayData;
  36. }
  37. // Get args number.
  38. $argCount = func_num_args();
  39. // Get keys to sort by and put them to SortRule array.
  40. for($i = 1;$i < $argCount;$i ++)
  41. {
  42. $arg = func_get_arg($i);
  43. if(!preg_match("/SORT/",$arg)){
  44. $keyNameList[] = $arg;
  45. $sortRule[] = '$'.$arg;
  46. }else{
  47. $sortRule[] = $arg;
  48. }
  49. }
  50. // Get the values according to the keys and put them to array.
  51. foreach($arrayData as $key => $info){
  52. foreach($keyNameList AS $keyName)
  53. {
  54. ${$keyName}[$key] = $info[$keyName];
  55. }
  56. }
  57. // Create the eval string and eval it.
  58. $evalString = 'array_multisort(' . join(",", $sortRule) . ',$arrayData);';
  59. eval ($evalString);
  60. return $arrayData;
  61. }
  62. }