note 118150 added to function.uksort

From: Date: Wed, 14 Oct 2015 19:15:58 +0000
Subject: note 118150 added to function.uksort
Groups: php.notes 
Request: Send a blank email to php-notes+get-203958@lists.php.net to get a copy of this message
Sort the indices by a set order (custom order) <?php function sort_keys(&$array, $order) { uksort($array, function ($a, $b) use ($order) { $pos_a = array_search($a, $order); $pos_b = array_search($b, $order); if ($pos_a === false) return 1; if ($pos_b === false) return -1; return $pos_a - $pos_b; }); return $array; } $order = ['a', 'c', 'z']; $arr = ['a'=>1, 'b'=>2, 'c'=>3, 'f'=>4, 'z'=>5]; sort_keys($arr, $order); // => [ // "a" => 1, // "c" => 3, // "z" => 5, // "b" => 2, // "f" => 4 // ] ---- Server IP: 72.52.91.14 Probable Submitter: 187.181.114.159 ---- Manual Page -- http://php.net/manual/en/function.uksort.php Edit -- https://master.php.net/note/edit/118150 Del: integrated -- https://master.php.net/note/delete/118150/integrated Del: useless -- https://master.php.net/note/delete/118150/useless Del: bad code -- https://master.php.net/note/delete/118150/bad+code Del: spam -- https://master.php.net/note/delete/118150/spam Del: non-english -- https://master.php.net/note/delete/118150/non-english Del: in docs -- https://master.php.net/note/delete/118150/in+docs Del: other reasons-- https://master.php.net/note/delete/118150 Reject -- https://master.php.net/note/reject/118150 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#203958) next »