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 attribute is used in <img> tag for the address of the image?
href src rel
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which CSS code hides the element on page?
display: none; display: inline; position: relative;
#id {
  display: none;
}
What instruction stops the execution of a while() or for() statement?
continue prompt() break
for(var i = 0; i< 8; i++) {
  if(i > 1) break;
  alert(i);
}
Indicate the function that can create a constant.
define() include() defined()
define("CONSTANT_NAME", "value");
echo CONSTANT_NAME;
Sort an entire multi-dimensional Array

Last accessed pages

  1. XML sitemap with data from MySQL (3545)
  2. Display multiple groups of images (5298)
  3. Merge Multiple Files, Line by Line (1009)
  4. SSEP - Site Search Engine PHP-Ajax (11279)
  5. Highlight Images on click (6537)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (443)
  2. SHA1 Encrypt data in JavaScript (236)
  3. Read Excel file data in PHP - PhpExcelReader (217)
  4. PHP Unzipper - Extract Zip, Rar Archives (217)
  5. Get and Modify content of an Iframe (139)