Showing posts with label PHP array_multisort. Show all posts
Showing posts with label PHP array_multisort. Show all posts

Thursday, 30 August 2018

PHP sorting table based on three internal table key values


I am looking to do some complex array sorting, but I have no idea where to start. The inner array has three relevant keys for sorting: first the year (numerical ASC), then the month (numerical ASC) and finally the name (alphabetical DESC).


<?php
// the current array:
$array = (
  array('year'=>2012, 'month'=>3, 'name'=>'John', 'score'=>12),
  array('year'=>2013, 'month'=>8, 'name'=>'Paul', 'score'=>3),
  array('year'=>2013, 'month'=>5, 'name'=>'Dennis', 'score'=>7),
  array('year'=>2012, 'month'=>3, 'name'=>'Paul', 'score'=>5),
  array('year'=>2012, 'month'=>12, 'name'=>'Paul', 'score'=>9),
  array('year'=>2012, 'month'=>9, 'name'=>'Mitt', 'score'=>3)
);

// I want to do some sorting with this as output:
$array = (
  array('year'=>2012, 'month'=>3, 'name'=>'John', 'score'=>12),
  array('year'=>2012, 'month'=>3, 'name'=>'Paul', 'score'=>5),
  array('year'=>2012, 'month'=>9, 'name'=>'Mitt', 'score'=>3),
  array('year'=>2012, 'month'=>12, 'name'=>'Paul', 'score'=>9),
  array('year'=>2013, 'month'=>5, 'name'=>'Dennis', 'score'=>7),
  array('year'=>2013, 'month'=>8, 'name'=>'Paul', 'score'=>3)
);
?>

If anyone can point me in the right direction that is really appreciated ;-).

This should work...
<?php
    // Obtain a list of columns
    // PHP 5 >= 5.5.0
    $years  = array_column($array, 'year');
    $months = array_column($array, 'month');
    $names  = array_map('strtolower', array_column($array, 'name')); // because it's a string sort.

    // Sort the data with volume descending, edition ascending
    // Add $data as the last parameter, to sort by the common key
    array_multisort($years, SORT_ASC, $months, SORT_ASC, $names, SORT_DESC, $array);
?>

EDIT : for PHP < 5.5
<?php
    // Obtain a list of columns
    // PHP < 5.5.0
    foreach ($array as $key => $row) {
        $years[$key]  = $row['year'];
        $months[$key] = $row['month'];
        $names[$key]  = strtolower($row['name']); // because it's a string sort.
    }
?>

Wednesday, 24 September 2014

array_multisort in PHP

array_multisort() function is utilized to sort multiple arrays or a multidimensional array by one or more extents.

Syntax:

array_multisort(array1,sorting order,sorting type,array2,array3...)
Parameters Description:
array1 :  Required. Specifies an array
sorting order : Optional. Specifies the sorting order.
Possible values:
  • SORT_ASC Default. Sort in ascending order (A-Z)
  • SORT_DESC sort in descending order (Z-A)
sorting type : Optional. Specifies the type to use, when comparing elements.
Possible values:
  • SORT_REGULAR Default. Compare elements normally
  • SORT_NUMERIC Compare elements as numeric values
  • SORT_STRING Compare elements as string values
array2 : Optional. Specifies an array
array3 : Optional. Specifies an array

Tip : You can assign only one array to the array_multisort() function, or as many as you like.
Note : String keys will be maintained, but numeric keys will be re-indexed, starting at 0 and increase by 1.
Note : You can assign the sorting order and the sorting type parameters after each array. If you don't, each array parameter uses the default values.

Example :

<?php
$daysarr1 = array( "Sunday","Monday" );
$daysarr2 = array("Tuesday","Wednesday");
array_multisort($daysarr1,$daysarr2);
echo "<br />"."simple example of multisort";
print_r($daysarr1);
print_r($daysarr2);
$daysarr1 = array("Sunday","Sunday","Monday");
$daysarr2 = array("Wednesday","Thursday","Friday");
array_multisort($daysarr1,$daysarr2);
echo "<br />"."using multisort when two values are same";
print_r($daysarr1);
print_r($daysarr2);
$daysarr1 = array("Sunday","Sunday',"Monday");
$daysarr2 = array("Wednesday","Thursday","Friday");
array_multisort($daysarr1,SORT_ASC,$daysarr2,SORT_DESC);
echo "<br />"."using sorting parameters";
print_r($daysarr1);
print_r($daysarr2);
?>

O/P:


simple example of multisort
Array (
         [0] => Monday
         [1] => Sunday 
        )
Array ( 
        [0] => Wednesday 
        [1]=> Tuesday
       )
using multisort when two values are same
Array (
         [0] => Monday
         [1] => Sunday
         [2] => Sunday
       )
Array (
         [0] => Friday 
         [1] => Thursday
         [2] => Wednesday
        )
using sorting parameters
Array ( 
         [0] => Monday
         [1] => Sunday 
         [2] => Sunday
        )
Array (
         [0] => Friday
         [1] => Thursday
         [2] => Wednesday
        )