PHP按字段排序数组?

标签 php arrays sorting

<分区>

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

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

数组结构可能是这样的:

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'
);

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

最佳答案

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

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

usort($array, "cmp");

关于PHP按字段排序数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10000005/

相关文章:

减法时PHP浮点计算错误

php - 使用 PHP 数组中的值

javascript - Node - 访问多维数组的最后一个索引

c# - 如何生成 HashMap 的 N 个排序键的列表(按值排序)?

php按列名对多维数组进行排序

php - instanceof 闭包返回 false

php - 在 AJAX 调用中从 PHP 返回一个 JSON 对象?

python - 如何指定用于在 python 中对列表进行排序的算法

php - 使用 php-cs-fixer 修复 PHP 代码文件的缩进

java - 在 Java 与 Python 中将字节转换为整数