在值的特定索引的基础上对整个数组进行排序

时间:2017-12-04 13:08:27

标签: php

我是根据特定的id从多个表中获取一些数据,而不是使用连接我使用嵌套查询,现在数据需要排序,因为我使用嵌套查询很难使用{{1}现在下面是我正在生成的数组!

orderby

基本上我正在使用一个循环来显示在上面的数据中检索到的信息,我想要做的是在循环内显示排序数据索引Array ( [0] => Array ( [id] => 54 [name] => Array ( [0] => Array ( [name] => Jumbo Green Raisins Chinese ) ) [path] => Array ( [0] => Array ( [path] => Jumbo_Green_Raisins_Chinese2.jpg ) ) [sort] => Array ( [0] => Array ( [sort] => 2 ) ) ) [1] => Array ( [id] => 55 [name] => Array ( [0] => Array ( [name] => Regular Green Raisins Chinese ) ) [path] => Array ( [0] => Array ( [path] => Regular_Green_Raisins_Chinese.jpg ) ) [sort] => Array ( [0] => Array ( [sort] => 4 ) ) ) [2] => Array ( [id] => 56 [name] => Array ( [0] => Array ( [name] => Green Sunderkhani Raisins ) ) [path] => Array ( [0] => Array ( [path] => Green_Sunderkhani_Raisins.jpg ) ) [sort] => Array ( [0] => Array ( [sort] => 3 ) ) ) [3] => Array ( [id] => 57 [name] => Array ( [0] => Array ( [name] => Jumbo Black Raisins ) ) [path] => Array ( [0] => Array ( [path] => Jumbo_Black_Raisins.jpg ) ) [sort] => Array ( [0] => Array ( [sort] => 1 ) ) ) ) 是我在排序时需要看到的,我可以也可以用db来做,但是我必须在很多地方弄乱代码,如果我这样做的话会更好!有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

基本的php usort

function sorted_nested($a, $b) {
    if($a['sort'][0]['sort'] == $b['sort'][0]['sort']) {
        return 0;
    }

    if($a['sort'][0]['sort'] < $b['sort'][0]['sort']) {
        return -1;
    } else {
        return 1;
    }
}

usort($array, 'sorted_nested');