Friday 26 June 2015

Sort an entire multi-dimensional Array

/*
 Sorts an entire multi-dimensional array by one element of it
 - $arr = the multi-dimensional array
 - $k = a string with the name of the index-key by which the entire array will be sorted
 - $sort = one of these sorting type flags:

    SORT_ASC - sort items ascendingly.
    SORT_DESC - sort items descendingly.
    SORT_REGULAR - compare items normally (don't change types)
    SORT_NUMERIC - compare items numerically
    SORT_STRING - compare items as strings
    SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
    SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
*/
function sortMultiArray($arr, $k, $sort) {
  $tmp = Array();
  foreach($arr as &$ma)  $tmp[] = &$ma[$k];
  $tmp = array_map('strtolower', $tmp);      // to sort case-insensitive
  array_multisort($tmp, $sort, $arr);
  return $arr;
}
- Example usage. Sorts the following array, by "nr", descendingly; and by "fruit".
$arr = array(
 'alex'=>array('nr'=>3, 'fruit'=>'orange'),
 'mars'=>array('nr'=>8, 'fruit'=>'Banana'),
 'victor'=>array('nr'=>4, 'fruit'=>'apple'),
);
Code:
<?php
// http://coursesweb.net/php-mysql/
/*
 Sorts an entire multi-dimensional array by one element of it
 - $arr = the multi-dimensional array
 - $k = a string with the name of the index-key by which the entire array will be sorted
 - $sort = one of these sorting type flags:

    SORT_ASC - sort items ascendingly.
    SORT_DESC - sort items descendingly.
    SORT_REGULAR - compare items normally (don't change types)
    SORT_NUMERIC - compare items numerically
    SORT_STRING - compare items as strings
    SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
    SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
*/
function sortMultiArray($arr, $k, $sort) {
  $tmp = Array();
  foreach($arr as &$ma)  $tmp[] = &$ma[$k];
  $tmp = array_map('strtolower', $tmp);      // to sort case-insensitive
  array_multisort($tmp, $sort, $arr);
  return $arr;
}

// two-dimensional array
$arr = array(
 'alex'=>array('nr'=>3, 'fruit'=>'orange'),
 'mars'=>array('nr'=>8, 'fruit'=>'Banana'),
 'victor'=>array('nr'=>4, 'fruit'=>'apple'),
);

// sorts the array by 'nr', descendingly
$arr1 = sortMultiArray($arr, 'nr', SORT_DESC);

// sorts the array by 'fruit'
$arr2 = sortMultiArray($arr, 'fruit', SORT_STRING);

// Test
echo '<pre>By "nr":<br/>';
var_export($arr1);

echo '<br/><br/>By "fruit":<br/>';
var_export($arr2);
echo '</pre>';
Results:
By "nr":
array (
  'mars' => array ( 'nr' => 8, 'fruit' => 'Banana' ),
  'victor' => array ( 'nr' => 4, 'fruit' => 'apple' ),
  'alex' => array ( 'nr' => 3, 'fruit' => 'orange' )
)

By "fruit":
array (
  'victor' => array ( 'nr' => 4, 'fruit' => 'apple' ),
  'mars' => array ( 'nr' => 8, 'fruit' => 'Banana' ),
  'alex' => array ( 'nr' => 3, 'fruit' => 'orange' )
)

The sortMultiArray() function performs a case-insensitive sorting. To perform a case-sensitive sorting (strings starting with a capital letter to come before strings starting with a lowercase letter) remove (or comment) this line of code from the function.
    $tmp = array_map('strtolower', $tmp);     // to sort case-insensitive

0 comments:

Post a Comment