usort

Keeping array index key when sorting a multidimensional array with PHP

蓝咒 提交于 2019-11-30 17:13:34
array(10) { [1019]=> array(3) { ["quantity"]=> int(0) ["revenue"]=> int(0) ["seller"]=> string(5) "Lenny" } [1018]=> array(3) { ["quantity"]=> int(5) ["revenue"]=> int(121) ["seller"]=> string(5) "Lenny" } [1017]=> array(3) { ["quantity"]=> int(2) ["revenue"]=> int(400) ["seller"]=> string(6) "Anette" } [1016]=> array(3) { ["quantity"]=> int(25) ["revenue"]=> int(200) ["seller"]=> string(6) "Samuel" } [1015]=> array(3) { ["quantity"]=> int(1) ["revenue"]=> int(300) ["seller"]=> string(6) "Samuel" } [1014]=> array(3) { ["quantity"]=> string(2) "41" ["revenue"]=> string(5) "18409" ["seller"]=>

Access array key using uasort in PHP

拈花ヽ惹草 提交于 2019-11-30 09:15:34
If have a rather basic uasort function in PHP that looks like this: uasort($arr, function($a, $b) { if ($a > $b) return -1; if ($a < $b) return 1; ... } The array I'm trying to sort looks like the following: {[1642] => 1, [9314] => 4, [1634] => 3 ...} It contains integers that are my main comparison criteria. However, if the integers are equal, then I would like to access their key values, inside the uasort function and do some magic with it to figure out the sorting from there. I have no clue how to do that as it seems that the $a and $b variables that get passed into the function are just

Sort Multi-dimensional array by decimal values

自闭症网瘾萝莉.ら 提交于 2019-11-29 14:00:53
What I'm trying to do is sort a multi-dimensional array that contains decimal values. From what I've tested, floats are having trouble being ordered properly. Array ( [0] => Array ( [company] => Ebay [weight] => 4.6 ) [1] => Array ( [company] => Ebay [weight] => 1.7 ) [2] => Array ( [company] => Ebay [weight] => 3.7 ) ) usort($array, 'order_by_weight'); // Sorts DESC highest first function order_by_weight($a, $b) { return $b['weight'] - $a['weight']; } What is the best way to sort these numbers in descending? $arr = array( array('company' => 'A', 'weight' => 4.6), array('company' => 'B',

Sort object(SimpleXMLElement) php

自作多情 提交于 2019-11-28 11:07:25
问题 I'm trying to find a way to sort my array from SimpleXMLElement. I'd like to sort by start time which I can get from event_start_dt. I'd also like to sort by room ID as a separate process. Currently the array is in order by object(SimpleXMLElement) #. Here is the var_dump($array): object(SimpleXMLElement)#275 (1) { ["reservation"]=> array(3) { [0]=> object(SimpleXMLElement)#287 (28) { ["reservation_id"]=> string(7) "8644894" ["event_start_dt"]=> string(25) "2013-12-02T12:00:00-08:00" ["event

Order this array by date modified?

社会主义新天地 提交于 2019-11-28 10:10:41
问题 I have a php file that is creating a array of everything in my users directory, the array is then being sent back to a iPhone. The array that my php is creating is ordering them alphabetically, i want it to sort by the date the file was created.. Here is what my php file looks like <?php $username = $_GET['username']; $path = "$username/default/"; $files = glob("{$path}/{*.jpg,*.jpeg,*.png}", GLOB_BRACE); // output to json echo json_encode($files); ?> How would i do this? Thanks :) 回答1: Using

Using usort in php to sort an array of objects?

纵然是瞬间 提交于 2019-11-28 10:10:20
问题 I did look at usort, but am still a little confused... Here is what the $myobject object looks like: Array ( [0] => stdClass Object ( [tid] => 13 [vid] => 4 ) [1] => stdClass Object ( [tid] => 10 [vid] => 4 ) [2] => stdClass Object ( [tid] => 34 [vid] => 4 ) [3] => stdClass Object ( [tid] => 9 [vid] => 4 ) I saw this: function cmp( $a, $b ) { if( $a->weight == $b->weight ){ return 0 ; } return ($a->weight < $b->weight) ? -1 : 1; } usort($myobject,'cmp'); I'm trying to sort according to tid,

How do I sort a PHP array by an element nested inside?

ⅰ亾dé卋堺 提交于 2019-11-27 21:49:59
I have an array like the following: Array ( [0] => Array ( 'name' => "Friday" 'weight' => 6 ) [1] => Array ( 'name' => "Monday" 'weight' => 2 ) ) I would like to grab the last values in that array (the 'weight'), and use that to sort the main array elements. So, in this array, I'd want to sort it so the 'Monday' element appears before the 'Friday' element. You can use usort as: function cmp($a, $b) { return $a['weight'] - $b['weight']; } usort($arr,"cmp"); Can be done using an anonymous function . Also if your 'weight' is a string use one of the other returns (see the commented out lines): <

In php how does usort() function works

巧了我就是萌 提交于 2019-11-27 19:24:49
I have looked at the php documentation, tutorials online and none of them how usort is actually working. I have an example i was playing with below. $data = array( array('msg' => 'some text','month' => 11,'level' => 10), array('msg' => 'some text','month' => 5,'level' => 10), array('msg' => 'some text','month' => 8,'level' => 10), array('msg' => 'some text','month' => 12,'level' => 10), array('msg' => 'some text','month' => 2,'level' => 10), array('msg' => 'some text','month' => 3,'level' => 10), array('msg' => 'some text','month' => 4,'level' => 10), array('msg' => 'some text','month' => 7,

In php how does usort() function works

让人想犯罪 __ 提交于 2019-11-27 04:22:06
问题 I have looked at the php documentation, tutorials online and none of them how usort is actually working. I have an example i was playing with below. $data = array( array('msg' => 'some text','month' => 11,'level' => 10), array('msg' => 'some text','month' => 5,'level' => 10), array('msg' => 'some text','month' => 8,'level' => 10), array('msg' => 'some text','month' => 12,'level' => 10), array('msg' => 'some text','month' => 2,'level' => 10), array('msg' => 'some text','month' => 3,'level' =>

Sorting multidim array: prioritize if column contains substring, then order by a second column

时光总嘲笑我的痴心妄想 提交于 2019-11-26 21:06:26
I am currently creating a sorting method that consists of values from an mysql query. Here's a brief view of the array: Array ( [0] => Array ( ['id'] = 1; ['countries'] = 'EN,CH,SP'; ) [1] => Array ( ['id'] = 2; ['countries'] = 'GE,SP,SV'; ) ) I have succeeded in making a normal usort based on the numeric id values, but I rather want to sort the array by the content of the "countries" field (if it contains a set string, a country code in this case), and then by the id field. The following snippet was my first idea of how to do it, but I have no idea of how to incorporate it into an working