The function presented in this page (sortMultiArray() ) can be used to sort /order an entire multi-dimensional array by one element of it, case-insensitive. Returns the sorted multi-dimensional array.
    - Code of the function:
/*
 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

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
}
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
alert(content);
Click on the "echo" correct instruction.
echo "CoursesWeb.net" echo "CoursesWeb.net"; echo ""CoursesWeb.net";
echo "Address URL: http://CoursesWeb.net";
Sort an entire multi-dimensional Array

Last accessed pages

  1. Butterfly Kyodai (6560)
  2. Bubbles3 (8623)
  3. Zuma Deluxe (9622)
  4. Qwop (3126)
  5. Get and change IFrame content through a JavaScript script created in another IFrame (6070)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (47733)
  2. Read Excel file data in PHP - PhpExcelReader (35866)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (35489)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (31998)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (29264)