我有一个如下所述的数组
Array
(
[6] => Array
(
[name] => Extras
[total_products] => 0
[total_sales] => 0
[total_affiliation] => 0
)
[5] => Array
(
[name] => Office Products
[total_products] => 7
[total_sales] => 17
[total_affiliation] => 8
)
[1] => Array
(
[name] => Hardware Parts
[total_products] => 6
[total_sales] => 0
[total_affiliation] => 0
)
)
Run Code Online (Sandbox Code Playgroud)
现在,订单是:额外,办公用品,硬件零件
我想按顺序排序主数组,它是按顺序排列的内部数组的total_sales
所以订单将是:办公用品,附加产品,硬件零件
任何帮助家伙
我需要php中的一个函数来根据任意顺序对单词列表进行排序.
列表中不以预定义顺序排列的任何单词都应按字母顺序排在列表末尾.
以下是我的第一次尝试,既不优雅也不高效.你能建议一个更好的方法来实现这个目标吗?
谢谢
public static function sortWords(&$inputArray){
$order=array("Banana","Orange", "Apple", "Kiwi");
sort($inputArray);
for($i=0;$i<count($inputArray));$i++){
$ac = $inputArray[$i];
$position = array_search($ac,$order);
if($position !== false && $i != $position){
$temp=$inputArray[$position];
$inputArray[$position]=$inputArray[$i];
$inputArray[$i]=$temp;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用usort对每个元素中的关联数组进行排序.
当我在数组中排序的所有值都相同时,它仍然会改变数组中元素的位置,有没有办法防止这种情况?
例如:
array(
array('name' => 'Ben', 'authn_weight' => 85.3),
array('name' => 'Josh', 'authn_weight' => 85.3),
array('name' => 'Fred', 'authn_weight' => 85.3)
);
Run Code Online (Sandbox Code Playgroud)
可以改为:
array(
array('name' => 'Josh', 'authn_weight' => 85.3),
array('name' => 'Ben', 'authn_weight' => 85.3),
array('name' => 'Fred', 'authn_weight' => 85.3)
);
Run Code Online (Sandbox Code Playgroud)
这是排序功能:
private function weightSortImplementation($a, $b){
$aWeight = $a['autn_weight'];
$bWeight = $b['autn_weight'];
if ($aWeight == $bWeight) {
return 0;
}
return ($aWeight < $bWeight) ? 1 : -1;
}
Run Code Online (Sandbox Code Playgroud)
我已经检查过该weightSortImplementation函数总是返回0表示它们是相同的.那为什么这仍然重新排序阵列?
我需要通过sububkey"description"升序对这个数组进行排序.我尝试了一些方法,如usort,ksort,subval_sort,但这些都没有工作(我猜主要问题是这些都是字符串,总是)
任何帮助表示赞赏
array(77) {
[0]=>
array(3) {
["name"]=>
string(17) "abcd"
["description"]=>
string(15) "Delete XY"
["level"]=>
int(1)
}
[1]=>
array(3) {
["name"]=>
string(13) "fgfgdgfd"
["description"]=>
string(18) "Uploader XY"
["level"]=>
int(1)
}
[2]=>
array(3) {
["name"]=>
string(15) "sdfdsfsdfs"
["description"]=>
string(20) "Download abc"
["level"]=>
int(0)
}
}
Run Code Online (Sandbox Code Playgroud) 我有下面的数组
Array
(
[0] => Array
(
[0] => 1280
[id] => 1280
)
[1] => Array
(
[0] => 2261
[id] => 2261
)
[2] => Array
(
[0] => 1280
[id] => 1280
)
)
Run Code Online (Sandbox Code Playgroud)
在php中,如何根据"id"的值从低到高排序?
我需要通过PHP脚本中的"得分"值对这类信息进行排序,我该怎么办?:
Array
(
[0] => stdClass Object
(
[name] => Morts par Déshydratation
[score] => 4
[id] => dwater
)
[1] => stdClass Object
(
[name] => Réparations de chantiers
[score] => 87
[id] => brep
)
[2] => stdClass Object
(
[name] => Campeur téméraire
[score] => 77
[id] => camp
)
[3] => stdClass Object
(
[name] => Décoration
[score] => 112
[id] => deco
)
)
Run Code Online (Sandbox Code Playgroud)
PS:这已经是PHP值,我已经使用过json_decode.*
我知道有一些关于使用多个条件进行排序的其他主题,但它们并不能解决我的问题.假设我有这个数组:
Array
(
[0] => Array
(
[uid] => 1
[score] => 9
[endgame] => 2
)
[1] => Array
(
[uid] => 2
[score] => 4
[endgame] => 1
)
[2] => Array
(
[uid] => 3
[score] => 4
[endgame] => 100
)
[3] => Array
(
[uid] => 4
[score] => 4
[endgame] => 70
)
)
Run Code Online (Sandbox Code Playgroud)
我想对它进行排序,将最高分的那个放在最前面.在同样的分数上,我想要一个最低端赛号码的人.排序机制应该将user1排在最前面,然后是user2,然后是4,然后是user3.
我使用这种排序机制:
function order_by_score_endgame($a, $b)
{
if ($a['score'] == $b['score'])
{
// score is the same, sort by endgame
if …Run Code Online (Sandbox Code Playgroud) 是否有更紧凑的方法来使用 PHP \xe2\x89\xa5 7.0按两个参数/字段对数组进行排序(使用spaceship 运算符) <=>?
现在我要排序的技巧是首先按第二个参数,然后按第一个参数:
\n// Sort by second parameter title\nusort($products, function ($a, $b) {\n return $a[\'title\'] <=> $b[\'title\']; // string\n});\n\n// Sort by first parameter brand_order\nusort($products, function ($a, $b) {\n return $a[\'brand_order\'] <=> $b[\'brand_order\']; // numeric\n});\nRun Code Online (Sandbox Code Playgroud)\n这给了我我想要的结果;产品首先按品牌排序,然后按名称排序。
\n我只是想知道是否有办法做到这一点usort。
这是我的问题作为代码片段。这个例子可以在这里测试。
\n<pre><?php\n \n<!-- Example array -->\n$products = array();\n\n$products[] = array("title" => "Title A", \n "brand_name" => "Brand B",\n "brand_order" => 1);\n$products[] = …Run Code Online (Sandbox Code Playgroud)我目前正在创建一个由mysql查询中的值组成的排序方法.
这是阵列的简要视图:
Array
(
[0] => Array
(
['id'] = 1;
['countries'] = 'EN,CH,SP';
)
[1] => Array
(
['id'] = 2;
['countries'] = 'GE,SP,SV';
)
)
Run Code Online (Sandbox Code Playgroud)
我已经成功地根据数字id值进行了正常的操作,但是我更愿意按"countries"字段的内容对数组进行排序(如果它包含一个设置字符串,在这种情况下是一个国家/地区代码),然后由id字段.
以下片段是我对如何做到的第一个想法,但我不知道如何将它合并到一个工作函数中:
in_array('EN', explode(",",$a['countries']) );
Run Code Online (Sandbox Code Playgroud)
你会怎么做?
谢谢!
不幸的是,我真的无处可去.
这就是我现在拥有的东西,它给我的只是错误: uasort() [function.uasort]: Invalid comparison function
function compare($a, $b) {
global $usercountry;
if ( in_array($usercountry, $a['countries']) && in_array($usercountry, $a['countries']) ) {
$return = 0;
}
else if (in_array($usercountry, $a['countries'])) {
$return = 1;
}
else {
$return = -1;
}
return $return;
}
$array= usort($array, …Run Code Online (Sandbox Code Playgroud) 我可以对多维数组进行排序,但不保持数字索引关联.
如何保持数字索引关联?
码:
$waiters[76] = array('weight' => 67, 'specialties' => 1);
$waiters[14] = array('weight' => 41, 'specialties' => 2);
$waiters[58] = array('weight' => 85, 'specialties' => 3);
$waiters[89] = array('weight' => 98, 'specialties' => 4);
$waiters[68] = array('weight' => 86, 'specialties' => 5);
$waiters[31] = array('weight' => 13, 'specialties' => 6);
print_r($waiters);
// Obtain a list of waiters
foreach ($waiters as $id => $waiter) {
$weight[$id] = $waiter['weight'];
$specialties[$id] = $waiter['specialties'];
}
// Sort the data with weight descending, specialties …Run Code Online (Sandbox Code Playgroud)