usort

PHP Sort an array alphabetically except ONE value on top, for a dropdown menu

对着背影说爱祢 提交于 2019-12-06 11:58:20
问题 I am trying to hack a dropdown menu pulling info from an array of countries so that 'United States' (id 1) appears on the top, while every other country is sorted by alphabetical order. How do I sort all EXCEPT United States to remain on top, using usort function for an array? Any alternative suggestions are also welcome. Here is the code: while (list($key, $value) = each($countries->countries)) { $countries_array[] = array('id' => $key, 'text' => $value['countryname']); } function text_cmp(

usort changing Array's order

前提是你 提交于 2019-12-05 16:23:05
I have a usort function with a single line: return 0. I tried to use it on an Array of stdClass objects, and it changes their order, how is that possible? The property you assume is called stability : A stable sorting algorithm will not change the order of elements that are equal. php's sorting functions are not stable (because non-stable sorts may be slightly faster). From the documentation of usort : If two members compare as equal, their order in the sorted array is undefined. If you want a stable sorting algorithm, you must implement it yourself . J0HN It's because that function means "I

PHP usort reorders array the sort value is the same for all

99封情书 提交于 2019-12-05 05:15:40
I'm using usort to sort an array with an associative array within each element. When all of the values I am sorting on in the array are the same then it still changes the position of the elements in the array, is there a way to prevent this? For example this: array( array('name' => 'Ben', 'authn_weight' => 85.3), array('name' => 'Josh', 'authn_weight' => 85.3), array('name' => 'Fred', 'authn_weight' => 85.3) ); May be changed to this: array( array('name' => 'Josh', 'authn_weight' => 85.3), array('name' => 'Ben', 'authn_weight' => 85.3), array('name' => 'Fred', 'authn_weight' => 85.3) ); This

PHP Sort an array alphabetically except ONE value on top, for a dropdown menu

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 18:02:11
I am trying to hack a dropdown menu pulling info from an array of countries so that 'United States' (id 1) appears on the top, while every other country is sorted by alphabetical order. How do I sort all EXCEPT United States to remain on top, using usort function for an array? Any alternative suggestions are also welcome. Here is the code: while (list($key, $value) = each($countries->countries)) { $countries_array[] = array('id' => $key, 'text' => $value['countryname']); } function text_cmp($a, $b) { return strcmp($a["text"], $b["text"]); } usort($countries_array, 'text_cmp'); The easiest way

Sort Multi-dimensional array by decimal values

∥☆過路亽.° 提交于 2019-12-03 18:08:12
问题 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

PHP's USORT Callback Function Parameters

大兔子大兔子 提交于 2019-12-03 12:13:30
问题 This is a really esoteric question, but I'm genuinely curious. I'm using usort for the first time today in years, and I'm particularly interested in what exactly is going on. Suppose I've got the following array: $myArray = array(1, 9, 18, 12, 56); I could sort this with usort: usort($myArray, function($a, $b){ if ($a == $b) return 0; return ($a < $b) ? -1 : 1; }); I'm not 100% clear about what is going on with the two parameters $a and $b. What are they, and what do they represent. I mean, I

Sorting and grouping SimpleXML Data

怎甘沉沦 提交于 2019-12-02 09:07:54
问题 I am sorting & grouping publication data from an XML file. The methods I am currently using are working fine for the most part , although I feel like there is a more efficient way to do what I am trying to accomplish. Here is a sample of what the target nodes look like: <comic> <id>117</id> <mainsection> <series> <displayname>My Amazing Adventure</displayname> <sortname>My Amazing Adventure</sortname> </series> </mainsection> <issuenr>2</issuenr> <seriefirstletter> <displayname>M</displayname

Sorting and grouping SimpleXML Data

自古美人都是妖i 提交于 2019-12-02 06:07:07
I am sorting & grouping publication data from an XML file. The methods I am currently using are working fine for the most part , although I feel like there is a more efficient way to do what I am trying to accomplish. Here is a sample of what the target nodes look like: <comic> <id>117</id> <mainsection> <series> <displayname>My Amazing Adventure</displayname> <sortname>My Amazing Adventure</sortname> </series> </mainsection> <issuenr>2</issuenr> <seriefirstletter> <displayname>M</displayname> <sortname>M</sortname> </seriefirstletter> </comic> Here are the current steps I am taking. Loading

How should I sort this array by key with usort?

笑着哭i 提交于 2019-12-01 18:59:24
I think I might have read every usort article on StackOverflow, but I can't work out this one. It might be that usort is not the tool I need? Here's a bit of the array that I'm working with (I have it assigned to $allPages ): Array ( [0] => Page Object ( [id] => 4 [slug] => articles [created_on] => 2009-08-06 07:16:00 ) [1] => Page Object ( [id] => 99 [slug] => a-brief-history [created_on] => 2011-04-25 12:07:26 ) [2] => Page Object ( [id] => 98 [slug] => we-arrive [created_on] => 2011-04-24 13:52:35 ) [3] => Page Object ( [id] => 83 [slug] => new-year [created_on] => 2011-01-02 14:05:12 ) ) I

How should I sort this array by key with usort?

坚强是说给别人听的谎言 提交于 2019-12-01 18:08:19
问题 I think I might have read every usort article on StackOverflow, but I can't work out this one. It might be that usort is not the tool I need? Here's a bit of the array that I'm working with (I have it assigned to $allPages ): Array ( [0] => Page Object ( [id] => 4 [slug] => articles [created_on] => 2009-08-06 07:16:00 ) [1] => Page Object ( [id] => 99 [slug] => a-brief-history [created_on] => 2011-04-25 12:07:26 ) [2] => Page Object ( [id] => 98 [slug] => we-arrive [created_on] => 2011-04-24