3.143.4.181 |    

Navigation

Google Advertisement

This function sort a multi dimensional array by key, order, type.

msort
  1. /******************************************************************* 
  2.  * This function sort a multi dimensional array by key, order, type.
  3.  * 
  4.  * @param array &$data referenz of a multi dimensional array
  5.  * @param string $keys sort by key name
  6.  * @param string $order order type (ASC|DESC)
  7.  * @param integer $type compare type (0..4)
  8.  * @return void
  9.  *******************************************************************/
  10. function msort ( &$data, $keys = 0, $order = "ASC", $type = 0 ) {
  11.  
  12. // Get the compare type
  13. switch($type) {
  14. 	case 1: // Case insensitive natural
  15. 		$compare = 'strcasenatcmp'; break;
  16. 	case 2: // Case sensitive natural
  17. 		$compare = 'strnatcmp'; break;
  18. 	case 3: // Case sensitive string
  19. 		$compare = 'strcmp'; break;
  20. 	case 4: // Case insensitive string
  21. 		$compare = 'strcasecmp'; break;
  22. 	default: // Default: Numeric
  23. 		$compare = Null; break;
  24. }
  25.  
  26. // Get the order type (ASC|DESC)
  27. if ( $order == "DESC" ) { // DESC:  >
  28. 	if ( isset($compare) )
  29. 		(string) $body = $compare.'( $b['.$keys.'], $a['.$keys.'] )';
  30. 	else
  31. 		(string) $body = '$b['.$keys.'] - $a['.$keys.']';
  32. } else { // ASC: <
  33. 	if ( isset($compare) )
  34. 		(string) $body = $compare.'( $a['.$keys.'], $b['.$keys.'] )';
  35. 	else
  36. 		(string) $body = '$a['.$keys.'] - $b['.$keys.']';
  37. }
  38.  
  39. // Sort the array data by a defined user function
  40. uasort( $data, create_function( '$a, $b', 'return ('.$body.');' ) );
  41.  
  42. }
  43.  
  44.  
  45. // @example
  46. $arr = array ( 10 => array('id' => 'dix',  'aa' => '1010'),
  47.                 100 => array('id' => 'cent', 'aa' => '100100'),
  48.                   2 => array('id' => 'deux', 'aa' => '22'),
  49.                   7 => array('id' => 'sept', 'aa' => '77'));
  50.  
  51.  
  52. header( 'Content-Type: text/plain' );
  53. print_r( msort( $arr, 'id', 'ASC', 3 ) );
Parsed in 0.011 seconds at 158.62 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 6 users online.

Tags Cloud

Latest snippets
str2seconds
(Bash::Function)
is_integer
(Bash::Function)
file_rotate
(Bash::Function)
confirm
(Bash::Function)
is_workingtime
(Bash::Function)
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)