usort

Possible to pass a closure to usort in PHP?

冷暖自知 提交于 2021-02-16 04:55:41
问题 I have an array sorting function as follows: public function sortAscending($accounts) { function ascending($accountA, $accountB) { if ($accountA['AmountUntilNextTarget'] == $accountB['AmountUntilNextTarget']) { return 0; } return ($accountA['AmountUntilNextTarget'] < $accountB['AmountUntilNextTarget']) ? -1 : 1; } usort($accounts, $ascending); return $accounts; } Clearly this is not ideal as it is hard-coding the key to search for. I thought I would make this generic by passing the key as a

usort multisort array based on another array

扶醉桌前 提交于 2021-02-15 07:36:46
问题 I have array that I need to sort by another array form 2 fields zip code and approve I am able to sort it by zip code but unable to do it with approved field so for eg I need to sort by 60007,60001,60003,60002 (as per sortlike order) all zip code from 60007 and approved should come first so $sortLike=array(60007,60001,60003,60002); $array1= array( array ('ID' => 138,'zip_code' => 60007,'approved' => 1), array('ID' => 103,'zip_code' => 60007,'approved' => 0), array('ID' => 114,'zip_code' =>

Toggle onclick between ascending en descending date using PHP

五迷三道 提交于 2021-02-10 14:41:05
问题 I have got this problem. I am trying to make a function where I can sort results by dates in a table. So far I got this: function wedstrijdenClub (){ $laMatches = WaterpoloAPI::call("Matches", "getMatches", Array( "", "", isset($_GET["ClubId"]) ? $_GET["ClubId"] : "", "", "", )); $asc = $laMatches; $desc = $laMatches ; // Sort Matches ascending usort($desc, function($a, $b) { return stringToUnix($a->Date) - stringToUnix($b->Date); }); // Sort Matches descending usort($asc, function($a, $b) {

Sorting an array of strings by arbitrary numbers of substrings

对着背影说爱祢 提交于 2020-12-06 08:27:51
问题 I'm attempting to modify the OrderedImportsFixer class in php-cs-fixer so I can clean up my files the way I want. What I want is to order my imports in a fashion similar to what you'd see in a filesystem listing, with "directories" listed before "files". So, given this array: $indexes = [ 26 => ["namespace" => "X\\Y\\Zed"], 9 => ["namespace" => "A\\B\\See"], 3 => ["namespace" => "A\\B\\Bee"], 38 => ["namespace" => "A\\B\\C\\Dee"], 51 => ["namespace" => "X\\Wye"], 16 => ["namespace" => "A\\Sea

Is it possible to usort a multidimensional array in PHP to be ordered by alternative values, i.e 1/0/1/0/1/0

你离开我真会死。 提交于 2020-03-03 07:40:58
问题 If you have this PHP array: $args = array( 'a' => array( 'order' => 1, ), 'b' => array( 'order' => 0, ), 'c' => array( 'order' => 0, ), 'd' => array( 'order' => 0, ), 'e' => array( 'order' => 1, ), ); Is there a way to use usort (or other) method that can order it by multidimensional key's value, but instead of being "incremental" (0,0,0,1,1), they would alternate (0,1,0,1,0). So using the array above as an example, the desired order output is for the keys to be ordered by alternate "order"

How do I sort a flat Json by Date?

十年热恋 提交于 2020-01-25 03:41:09
问题 I am pretty new to php and Jsons and I am trying to arrange contents of a Json by date. I know about the usort() function but so far I have been unsuccessful at using it. This is the Json: [{"id":"1","poi_id":"36","date":"1993-05-14","url":"#","p_flag":"0"},{"id":"2","poi_id":"36","date":"2000-05-14","url":"#","p_flag":"0"},{"id":"3","poi_id":"36","date":"1992-05-14","url":"#","p_flag":"0"}] What I have been trying to do is this: function sortByYear($a, $b) { $dA = new DateTime($a['date']);

Sort 2nd nested array by value in 3rd with php

倾然丶 夕夏残阳落幕 提交于 2020-01-16 10:37:48
问题 Need to sort second nested array by a value n the third with php; 'Start Time'. Thinking maybe usort is the key. Posting the nested arrays of @week @day @class as well as a usort call and function I've been trying variations on. Array ( [Sunday] => Array ( [0] => Array ( [Class Name] => Fun Class [Instructor] => Fun Teacher [Class Type] => Yoga Stretches [Start Time] => 1899-12-30T17:00:00 [End Time] => 1899-12-30T18:15:00 [Day Name] => Sunday ) [1] => Array ( [Class Name] => Another Fun

PHP sort multidimensional array usort()

倖福魔咒の 提交于 2020-01-07 05:21:07
问题 I have the below array and want to order it alphbetically by "Name". I am a little confused on how to use the usort() function for this as what I have does not work, or is there a better function to use? Array ( [0] => SimpleXMLElement Object ( [id] => 1118809 [Name] => Laptop [parentID] => 0 [sequence] => 4 [visible] => 1 ) [1] => SimpleXMLElement Object ( [id] => 1109785 [Name] => Special Offers [parentID] => 0 [sequence] => 0 [visible] => 1 ) [2] => SimpleXMLElement Object ( [id] =>

Sort for Czech characters PHP - issue in compare function

独自空忆成欢 提交于 2020-01-06 14:42:05
问题 I'm making sorted sorting of Czech (and other) characters. I wrote a compare function but there is a problem with strstr: Warning: strstr() [function.strstr]: needle is not a string or an integer in ... And second problem is that it doesn't compare it correctly. I checked the manual of usort and some examples and think it should be ok, but something is wrong. I also check $a->getJmenoSouboru() and $b->getJmenoSouboru() on string and it is really string. So I don't understand to that warning.

Sort for Czech characters PHP - issue in compare function

我的未来我决定 提交于 2020-01-06 14:41:17
问题 I'm making sorted sorting of Czech (and other) characters. I wrote a compare function but there is a problem with strstr: Warning: strstr() [function.strstr]: needle is not a string or an integer in ... And second problem is that it doesn't compare it correctly. I checked the manual of usort and some examples and think it should be ok, but something is wrong. I also check $a->getJmenoSouboru() and $b->getJmenoSouboru() on string and it is really string. So I don't understand to that warning.