Php-mysql Course

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
// https://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
What attribute makes an option from <select> selected?
checked="checked" selected="selected" disabled="disabled"
<select name="a_name">
 <option value="val1">Option 1</option>
 <option value="val2" selected="selected">Option 2</option>
</select>
What CSS value allows to create color gradients for background?
contain repeat-x linear-gradient
#id {
  background: linear-gradient(top left, #1f1, #fff, #11f);
}
What statement creates an array in JavaScript?
[] {} new Object()
var arr = [1, "CoursesWeb.net", "MarPlo.net"];
alert(arr[2]);
Indicate the PHP function used to redirect to other page.
function() header() switch()
header("Location: http://coursesweb.net/");
exit;
Sort an entire multi-dimensional Array

Last accessed pages

  1. Objects in 3D Space (1963)
  2. Convert XML to JSON in JavaScript (33951)
  3. Zen Flesh, Zen Bones (807)
  4. Get Duration of Audio /Video file before Upload (15297)
  5. Multiple Select Dropdown List with JavaScript (13471)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (13)
  2. SSEP - Site Search Engine PHP-Ajax (7)
  3. Adobe Flash Courses ActionScript 3 Tutorials (3)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (3)
  5. Animation with the Timer class (3)