简体   繁体   English

如何使用Codeigniter按字母顺序对数组排序

[英]How to sort an array in alphabetical order using codeigniter

How to sort an array in alphabetical order using codeigniter 如何使用Codeigniter按字母顺序对数组排序

array(
     Array(

            [id] => 4
            [business_category] => Air_conditioning-Auto
            [business_name] => test business
        );
      Array(

            [id] => 55
            [business_category] => Air_conditioning-Auto
            [business_name] => asdf
        );
);

Answer taken from this post: Sort array of objects by object fields 这篇文章的答案: 按对象字段对对象数组进行排序

Use usort , here's an example adapted from the manual: 使用usort ,这是从手册改编而成的示例:

function cmp($a, $b)
{
    return strcmp($a->name, $b->name);
}

usort($your_data, "cmp");

If you're sorting the array from inside the class and your sorting function cmp is also defined inside the class, then use this: 如果要从类内部对数组进行排序,并且排序函数cmp也已在类内部定义,请使用以下命令:

usort($your_data, array($this, "cmp"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM