usort

PHP usort for string hierarchy

£可爱£侵袭症+ 提交于 2019-12-11 16:11:31
问题 I have a array with a string hierarchy like so: table, parent_table test, NULL test, NULL test2, test test4, NULL test5, test3 test6, test5 test3, test I want to sort with a function that goes something like this: usort($array, function($a,$b) { return ($a['table'] === $b['parent_table']) ? -1 : 1; }); Ideal result would be table, parent_table test, NULL test, NULL test2, test test3, test test5, test3 test6, test5 test4, NULL This would sort parents above children tables. I've been trying to

Does returning “-1” with usort really move the $b variable or does it keep it in the same place?

允我心安 提交于 2019-12-11 14:16:38
问题 A simple piece of code written by me: <?php function testing($a,$b){ if ($a < $b ){ return -1; } elseif ($a > $b){ return 1; } //else { //return 0; //} } $array = array(1,3,2,4,5); usort($array, "testing"); var_dump($array); ?> This is from the top comment (highest rated comment and from 5 years ago) on the php.net manual's usort page: "If you return -1 that moves the $b variable down the array, return 1 moves $b up the array and return 0 keeps $b in the same place." As far as I was looking

second sorting with php usort

别等时光非礼了梦想. 提交于 2019-12-11 06:12:49
问题 So Ive got a pretty big array of data and need to sort them by two criteria. There is variable $data['important'] and $data['basic'] . They are simple numbers and I am using uasort to sort $data firstly by important and then by basic. So Important | Basic 10 | 8 9 | 9 9 | 7 7 | 9 The usort function is a simple public function sort_by_important($a, $b) { if ($a[important] > $b[important]) { return -1; } elseif ($b[important] > $a[important]) { return 1; } else { return 0; } } How can I re-sort

Sorting laravel collection by leaving null/empty last

一曲冷凌霜 提交于 2019-12-11 05:25:20
问题 Can't seem to get my head around of sorting laravel collection so empty / null data would end up being last. ( bit confused about usort ) Pretty much all I have is bunch of times / timestamps that need to be ordered. Some rows may not have for that column. I would like data to appear ASC / ascending while empty/null data is shown last. $collection->sortBy('timestamp') sorts nicely but doesn't know how to deal with empty fields. Table looks like this. $data = $data->sort(function($a, $b) use (

usort not working for laravel multidimensional arrays

天大地大妈咪最大 提交于 2019-12-11 03:58:28
问题 I have an array Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => stdClass Object ( [id] => 79 [name] => shelin [status] => 0 ) [1] => stdClass Object ( [id] => 80 [name] => shanu [status] => 2 ) [2] => stdClass Object ( [id] => 81 [name] => linto [status] => 2 ) [3] => stdClass Object ( [id] => 82 [name] => joseph [status] => 0 ) ) ) I want to rearrange this array by status desc order I try usort($usersdetailsA, function($a, $b) { return $a->status <=> $b->status; })

PHP sorting by date

馋奶兔 提交于 2019-12-11 02:48:47
问题 I have tried to use usort but I am having issues I have now used the usort function. my array is has a string key and a string value which represents the date my output is as follows: 02/09/2013 03/09/2013 03/10/2013 04/07/2013 04/09/2013 09/09/2013 11/09/2013 13/06/2013 13/08/2013 It is only sorting by the first two numbers, I want it to sort for the full date, What am I doing wrong? This is my code: usort($filesWithDates,"my_sort"); foreach ($filesWithDates as &$value) { echo $value."<br/>"

PHP multidimensional array (usort)

时光毁灭记忆、已成空白 提交于 2019-12-10 16:18:33
问题 I have an associative array like this Array ( ["News 1"] => Array ( [text] => tests [language] => [advertiserCompetitionScale] => 5 [avgSearchVolume] => 7480000 [lastMonthSearchVolume] => 9140000 ) ["News 2"] => Array ( [text] => personality tests [language] => [advertiserCompetitionScale] => 5 [avgSearchVolume] => 165000 [lastMonthSearchVolume] => 201000 ) ["News 3] => Array ( [text] => online tests [language] => [advertiserCompetitionScale] => 5 [avgSearchVolume] => 246000

php二维数组排序方法(array_multisort usort)

半城伤御伤魂 提交于 2019-12-10 08:51:21
一维数组排序可以使用asort、ksort等一些方法进程排序,相对来说比较简单。二维数组的排序怎么实现呢?使用array_multisort和usort可以实现. 例如像下面的数组: 复制代码 代码如下: $users = array( array('name' => 'tom', 'age' => 20) , array('name' => 'anny', 'age' => 18) , array('name' => 'jack', 'age' => 22) ); 希望能按照age从小到大进行排序。笔者整理了两个方法出来,分享给大家。 1、使用array_multisort 使用这个方法,会比较麻烦些,要将age提取出来存储到一维数组里,然后按照age升序排列。具体代码如下: 复制代码 代码如下: $ages = array(); foreach ($users as $user) { $ages[] = $user['age']; } array_multisort($ages, SORT_ASC, $users); 执行后,$users就是排序好的数组了,可以打印出来看看。如果需要先按年龄升序排列,再按照名称升序排列,方法同上,就是多提取一个名称数组出来,最后的排序方法这样调用: 复制代码 代码如下: array_multisort($ages, SORT_ASC,

usort function result is wrong

久未见 提交于 2019-12-08 13:02:33
问题 Today I provided the answer of this question and I wrote a script, but I found out that something went wrong. Here is the first script <?php $array = array( "0" => array ( "id" => 1204, "custom_price" => 33.1500 ), "1" => array ( "id" => 1199, "custom_price" => 15.83 ), "2" => array ( "id" => 1176, "custom_price" => 16.83 ) ); usort($array, function($a, $b) { return $a['custom_price'] - $b['custom_price']; }); echo "<pre>"; print_r($array); and its output is (also you can check output on

Using usort with simplexml

自古美人都是妖i 提交于 2019-12-06 12:54:13
问题 I'm having a problem where none of my values are ending up in the right order. $xml = file_get_contents('admin/people.xml'); $x = new SimpleXMLElement($xml); $sort=$x->person; function cmp($a, $b){ if ($a->age == $b->age) { return 0; } return ($a->age < $b->age) ? -1 : 1; } usort($sort, 'cmp'); foreach ($sort as $key => $value) { echo "$key: $value->age<br>"; } From everything I've read, this should work, but it doesn't. Here is the XML: <people> <person> <name>Frank</name> <age>12</age> <