note 125960 added to function.array-multisort

From: Date: Thu, 25 Mar 2021 14:44:05 +0000
Subject: note 125960 added to function.array-multisort
Groups: php.notes 
Request: Send a blank email to php-notes+get-219147@lists.php.net to get a copy of this message
For database like sorting, here is my 2 cents: <?php /** * The RowsSortHelperTool class. */ class RowsSortHelperTool { /** * Sorts the given array, based on the given sorts. * * The sorts argument is an array of field => direction, * * with: * * - field: string, the name of the property to sort the rows with * - direction: string (asc|desc), the direction of the sort * * * @param array $rows * @param array $sorts * @return array */ public static function sort(array &$rows, array $sorts) { $args = []; foreach ($sorts as $field => $direction) { $col = array_column($rows, $field); $args[] = $col; if ('asc' === $direction) { $args[] = SORT_ASC; } else { $args[] = SORT_DESC; } } $args[] = &$rows; call_user_func_array("array_multisort", $args); } } ?> Use it like this: <?php $data[] = array('volume' => 67, 'edition' => 2, 'mine' => 5); $data[] = array('volume' => 86, 'edition' => 1, 'mine' => 5); $data[] = array('volume' => 85, 'edition' => 6, 'mine' => 5); $data[] = array('volume' => 98, 'edition' => 2, 'mine' => 5); $data[] = array('volume' => 86, 'edition' => 6, 'mine' => 4); $data[] = array('volume' => 86, 'edition' => 6, 'mine' => 5); $data[] = array('volume' => 67, 'edition' => 7, 'mine' => 5); RowsSortHelperTool::sort($data, [ 'volume' => 'desc', 'edition' => 'asc', 'mine' => 'desc', ]); az($data); ?> Will display something like this: array(7) { [0] => array(3) { ["volume"] => int(98) ["edition"] => int(2) ["mine"] => int(5) } [1] => array(3) { ["volume"] => int(86) ["edition"] => int(1) ["mine"] => int(5) } [2] => array(3) { ["volume"] => int(86) ["edition"] => int(6) ["mine"] => int(5) } [3] => array(3) { ["volume"] => int(86) ["edition"] => int(6) ["mine"] => int(4) } [4] => array(3) { ["volume"] => int(85) ["edition"] => int(6) ["mine"] => int(5) } [5] => array(3) { ["volume"] => int(67) ["edition"] => int(2) ["mine"] => int(5) } [6] => array(3) { ["volume"] => int(67) ["edition"] => int(7) ["mine"] => int(5) } } ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.247 (proxied: 143.244.57.69) ---- Manual Page -- http://php.net/manual/en/function.array-multisort.php Edit -- https://master.php.net/note/edit/125960 Del: integrated -- https://master.php.net/note/delete/125960/integrated Del: useless -- https://master.php.net/note/delete/125960/useless Del: bad code -- https://master.php.net/note/delete/125960/bad+code Del: spam -- https://master.php.net/note/delete/125960/spam Del: non-english -- https://master.php.net/note/delete/125960/non-english Del: in docs -- https://master.php.net/note/delete/125960/in+docs Del: other reasons-- https://master.php.net/note/delete/125960 Reject -- https://master.php.net/note/reject/125960 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#219147) next »