asort

Properly sorting multidimensional array using usort and spaceship operator

自古美人都是妖i 提交于 2019-12-31 04:02:29
问题 I need to sort the following array with following logic: If the 'score' is the same, I want to compare using 'time'. The array is as follows: $user_scores = [ 82 => [ 'score' => 1, 'time' => 6.442 ], 34 => [ 'score' => 1, 'time' => 5.646 ], 66 => [ 'score' => 3, 'time' => 1.554 ] ] The 'keys' in the above array are the 'user_ids', which I need to preserve in the sorted array. My best attempt so far is as below - $result = usort( $user_scores, function ( $a, $b ) { if ( $a['score'] == $b[

asort not working properly alphabetically

时间秒杀一切 提交于 2019-12-12 04:12:46
问题 I have a array like below. I am trying to sort the array alphabetically with asort. but it does not work Array ( [0] => BelExpo exhibition center [1] => Aurora cinema theatre [2] => Yunost Ice hockey palace [3] => Rakauski free market [4] => Grushevka Metro Station ) $arr = asort($arr1); // output gives 1 thanks in advance 回答1: OK great I solved the problem. sort return boolean value. no need to put it in variable. just pass the array in sort. and it will work sort($arr); print_r($arr); 来源:

PHP sort an array of arrays according to an index in the arrays

馋奶兔 提交于 2019-12-11 15:28:11
问题 Let's say you have an array that looks like this: $myArray []= array('firstname' => 'John' , 'lastname'=> 'Johnson'); $myArray []= array('firstname' => 'Adam' , 'lastname'=> 'Tyson'); $myArray []= array('firstname' => 'Mike' , 'lastname'=> 'Robinson'); $myArray []= array('firstname' => 'David' , 'lastname'=> 'Jackson'); How can you sort $myArray according to the lastname? 回答1: This also works <?php $myArray []= array('firstname' => 'John' , 'lastname'=> 'Johnson'); $myArray []= array(

asort PHP Array and keep one entry at the top

心已入冬 提交于 2019-12-11 14:44:46
问题 I have a PHP array I want to sort it alphabetically and keep a precise entry at the top: $arr = array ("Orange", "Banana", "Strawberry", "Apple", "Pear"); asort($arr); Now this will output: Apple, Banana, Orange, Pear, Strawberry I want it to keep Orange as the first entry then reorder the others: Orange, Apple, Banana, Pear, Strawberry Thanks. 回答1: Get first element from array, then return it back: $arr = array ("Orange", "Banana", "Strawberry", "Apple", "Pear"); $first = array_shift($arr);

How to sort values with special chars in array?

一曲冷凌霜 提交于 2019-12-11 09:15:05
问题 First, I set the proper locale to spanish: setlocale(LC_ALL, 'es_ES'); This array holds a list of languages which must be reordered alphabetically. $lang['ar'] = 'árabe'; $lang['fr'] = 'francés'; $lang['de'] = 'alemán'; So I do this: asort($lang,SORT_LOCALE_STRING); The final result is: alemán francés árabe ...and it should be: árabe alemán francés The asort() function is sending the á character to the bottom of the ordered list. How can I avoid this issue? Thanks! Solution linked by @Sbls

How to sort associative array using sub-field of contained associative arrays in PHP?

我们两清 提交于 2019-12-08 01:32:01
问题 How can I sort an associative array by one of its values? For example: $arr = array( 'ted' => array( 'age' => 27 ), 'bob' => array( 'age' => 18 ), 'jay' => array( 'age' => 24 ) ); $arr = ??? foreach ($arr as $person) echo $person['age'], ', '; So that the output is: 18, 24, 27 This is an oversimplified example just to demonstrate my question. I still require that $arr is an associative array. 回答1: The uasort() function allows you to specify a callback function, which will be responsible of

How to sort associative array using sub-field of contained associative arrays in PHP?

风格不统一 提交于 2019-12-06 10:01:18
How can I sort an associative array by one of its values? For example: $arr = array( 'ted' => array( 'age' => 27 ), 'bob' => array( 'age' => 18 ), 'jay' => array( 'age' => 24 ) ); $arr = ??? foreach ($arr as $person) echo $person['age'], ', '; So that the output is: 18, 24, 27 This is an oversimplified example just to demonstrate my question. I still require that $arr is an associative array. The uasort() function allows you to specify a callback function, which will be responsible of doing the comparison between two elements -- so, should do just well, if you implement the proper callback

bash, find nearest next value, forward and backward

余生颓废 提交于 2019-12-01 12:44:21
I have a data.txt file 1 2 3 4 5 6 7 cat data.txt 13 245 1323 10.1111 10.2222 60.1111 60.22222 13 133 2325 11.2222 11.333 61.2222 61.3333 13 245 1323 12.3333 12.4444 62.3333 62.44444444 13 245 1323 13.4444 13.5555 63.4444 63.5555 Find next nearest: My target value is 11.6667 and it should find the nearest next value in column 4 as 12.3333 Find previous nearest: My target value is 62.9997 and it should find the nearest previous value in column 6 as 62.3333 I am able to find the next nearest (case 1) by awk -v c=4 -v t=11.6667 '{a[NR]=$c}END{ asort(a);d=a[NR]-t;d=d<0?-d:d;v = a[NR] for(i=NR-1;i>

bash, find nearest next value, forward and backward

社会主义新天地 提交于 2019-12-01 12:02:14
问题 I have a data.txt file 1 2 3 4 5 6 7 cat data.txt 13 245 1323 10.1111 10.2222 60.1111 60.22222 13 133 2325 11.2222 11.333 61.2222 61.3333 13 245 1323 12.3333 12.4444 62.3333 62.44444444 13 245 1323 13.4444 13.5555 63.4444 63.5555 Find next nearest: My target value is 11.6667 and it should find the nearest next value in column 4 as 12.3333 Find previous nearest: My target value is 62.9997 and it should find the nearest previous value in column 6 as 62.3333 I am able to find the next nearest

Sort associative array with AWK

心不动则不痛 提交于 2019-11-30 06:46:12
Here's my array (gawk script) : myArray["peter"] = 32 myArray["bob"] = 5 myArray["john"] = 463 myArray["jack"] = 11 After sort, I need the following result : bob 5 jack 11 peter 32 john 463 When i use "asort", indices are lost. How to sort by array value without losing indices ? (I need ordered indices based on their values) (I need to obtain this result with awk/gawk only, not shell script, perl, etc) If my post isn't clear enough, here is an other post explaining the same issue : http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Q_26626841.html ) Thanks in advance Update