php PHP按字段对数组进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10000005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:07:06  来源:igfitidea点击:

PHP Sort array by field?

phparrayssorting

提问by Michael Ecklund

Possible Duplicate:
how to sort a multidemensional array by an inner key
How to sort an array of arrays in php?

可能的重复:
如何通过内部键对多维数组
进行排序 如何在 php 中对数组数组进行排序?

How can I sort an array like: $array[$i]['title'];

如何对数组进行排序,例如: $array[$i]['title'];

Array structure might be like:

数组结构可能是这样的:

array[0](
'id' => 321,
'title' => 'Some Title',
'slug' => 'some-title',
'author' => 'Some Author',
'author_slug' => 'some-author');

array[1](
'id' => 123,
'title' => 'Another Title',
'slug' => 'another-title',
'author' => 'Another Author',
'author_slug' => 'another-author');

So data is displayed in ASC orderbased off the title fieldin the array?

那么数据是根据数组中的标题字段ASC 顺序显示的吗?

回答by meagar

Use usortwhich is built explicitly for this purpose.

usort为此目的而明确构建的使用。

function cmp($a, $b)
{
    return strcmp($a["title"], $b["title"]);
}

usort($array, "cmp");