PHP排序 [英] PHP Sorting

查看:61
本文介绍了PHP排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对每个条目具有多个值的关联数组进行排序.

I am trying to sort an associative array which has multiple vales per entry.

例如

[0] => stdClass Object ( [type] => node [sid] => 158 [score] => 0.059600525242489 )
[1] => stdClass Object ( [type] => node [sid] => 247 [score] => 0.059600525242489 )

我想按分数"对数组进行排序(最高分是第一个索引)

I want the array sorted by 'score' (highest score is first index)

我该怎么做?

推荐答案

使用 usort函数具有此比较功能:

Use the usort function with this comparison function:

function cmpByScore($a, $b) {
    if ($a['score'] == $b['score']) {
        return 0;
    }
    return $a['score'] > $b['score'] ? 1 : -1;
}

usort($array, 'cmpByScore');

这篇关于PHP排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆