note 116264 added to function.usort

From: Date: Wed, 03 Dec 2014 15:10:40 +0000
Subject: note 116264 added to function.usort
Groups: php.notes 
Request: Send a blank email to php-notes+get-201056@lists.php.net to get a copy of this message
Below is a variation on Example #4 that implements a multi-key natural sort on an associative array and can be called in such a way as to allow reversing the sort order and/or making the comparison case insensitive on a key by key basis. Test code is included in the file – run it from the command line with: php sortUtils.php <?php /** * sortUtils.php */ /** * mknatsort() - Multi-Key Natural Sort for associative arrays * * Uses the usort() function to perform a natural sort on a multi-dimensional * array on multiple keys. Optionally specifying the sort order for each key * and/or ignoring the case for each key value. * * @param array $data_array The array to be sorted. * @param mixed $keys The list of keys to be sorted by. This may be a single * key or an array of keys * @param boolean $reverse Specify whether or not to reverse the sort order. If * there are multiple keys then $reverse may be an array of booleans - one * per key. * @param boolean $ignorecase Specify whether or not to ignore the case when * comparing key values.reverse the sort order. If there are multiple keys * then $ignorecase may be an array of booleans - one per key. */ function mknatsort ( &$data_array, $keys, $reverse=false, $ignorecase=false ) { // make sure $keys is an array if (!is_array($keys)) $keys = array($keys); usort($data_array, sortcompare($keys, $reverse, $ignorecase) ); } function sortcompare($keys, $reverse=false, $ignorecase=false) { return function ($a, $b) use ($keys, $reverse, $ignorecase) { $cnt=0; // check each key in the order specified foreach ( $keys as $key ) { // check the value for ignorecase and do natural compare accordingly $ignore = is_array($ignorecase) ? $ignorecase[$cnt] : $ignorecase; $result = $ignore ? strnatcasecmp ($a[$key], $b[$key]) : strnatcmp($a[$key], $b[$key]); // check the value for reverse and reverse the sort order accordingly $revcmp = is_array($reverse) ? $reverse[$cnt] : $reverse; $result = $revcmp ? ($result * -1) : $result; // the first key that results in a non-zero comparison determines // the order of the elements if ( $result != 0 ) break; $cnt++; } return $result; }; } // end sortcompare() // test code that will be skipped if this is included in the web environment // test with: php sortUtils.php from the command line $sapi_type = php_sapi_name(); if (substr($sapi_type, 0, 3) == 'cli') { // the following shows several examples of usage and the resulting output. $test[] = array('Status'=>'In Progress', 'comment'=>'This looks good.', 'user_name'=>'John Doe', 'date_modified'=>'06/13/2014 11:20 AM', 'role'=>'Senior Account Manager'); $test[] = array('Status'=>'In Progress', 'comment'=>'This looks good, please approve.', 'user_name'=>'John Doe', 'date_modified'=>'06/13/2014 11:19 AM', 'role'=>'Account Manager'); $test[] = array('Status'=>'In Progress', 'comment'=>'This looks good.', 'user_name'=>'Jane Smith', 'date_modified'=>'06/13/2014 11:19 AM', 'role'=>'Senior Account Manager'); $test[] = array('Status'=>'Returned', 'comment'=>'There is not enough informartion. Please add the following.', 'user_name'=>'John Doe', 'date_modified'=>'06/13/2014 11:15 AM', 'role'=>'Account Manager'); $test[] = array('Status'=>'In Progress', 'comment'=>'I am currently reviewing this.', 'user_name'=>'John Doe', 'date_modified'=>'06/13/2014 11:14 AM', 'role'=>'Account Manager'); echo "Original array: "; print_r($test); echo "Sorted on date_modified: "; mknatsort($test, array('date_modified') ); print_r($test); echo "Sorted on role(reversed) & date_modified: "; mknatsort($test, array('role','date_modified'),array(true,false) ); print_r($test); echo "Sorted on role & date_modified(reversed): "; mknatsort($test, array('role','date_modified'),array(false,true) ); print_r($test); } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 71.163.184.250 ---- Manual Page -- http://php.net/manual/en/function.usort.php Edit -- https://master.php.net/note/edit/116264 Del: integrated -- https://master.php.net/note/delete/116264/integrated Del: useless -- https://master.php.net/note/delete/116264/useless Del: bad code -- https://master.php.net/note/delete/116264/bad+code Del: spam -- https://master.php.net/note/delete/116264/spam Del: non-english -- https://master.php.net/note/delete/116264/non-english Del: in docs -- https://master.php.net/note/delete/116264/in+docs Del: other reasons-- https://master.php.net/note/delete/116264 Reject -- https://master.php.net/note/reject/116264 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#201056) next »