PHP数组排序随着多列数据 [英] PHP Array Sort With Multiple Columns of Data

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

问题描述

我想打一个数组排序方法,这样你就可以通过排序价格/身份证号码等一些信息。

I want to make an array sort method so you can sort some info via price/ID number etc.

我的数组是这样的:

[1] 1002234985, $123.00, ITEM DESCRIPTION #1

[2] 1034234985, $143.70, ITEM DESCRIPTION #2

[3] 1002467455, $133.06, ITEM DESCRIPTION #3

[4] 1564334985, $883.11, ITEM DESCRIPTION #4

我想按价格排序的数组,但描述和ID号匹配。我怎样才能做到这一点?

I want to sort the array by price, but have the descriptions and ID numbers match up. How can I do this?

谢谢!

推荐答案

比较功能(接收两个元素并返回哪一个更大),并使用<一个HREF =htt​​p://php.net/manual/en/function.usort.php相对=nofollow> usort 使用它

write the compare function (that receives two elements and returns which one is bigger) and use usort to use it

一个例子:

$arr = array(
              array(1002234985, '125.00', 'ITEM DESCRIPTION'), 
              array(1002234986, '124.00', 'ITEM DESCRIPTION'), 
              array(1002234987, '123.00', 'ITEM DESCRIPTION')
);
function mycomp($itm1, $itm2){
    if($itm1[1] > $itm2[1]){
        return 1;
    }
    else if($itm1[1] < $itm2[1]){
        return -1;
    }
    else{
        return 0;
    }
}
usort($arr, 'mycomp');
print_r($arr);

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

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