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
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Sort an entire multi-dimensional Array

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141763)
  2. Download PHP-MySQL resources (1156)
  3. AJAX and XML (2351)
  4. PHP PDO - Introduction and Connecting to Databases (8631)
  5. Laravel Basic Architecture (1280)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (487)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (73)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)