PHP按字段对数组进行排序? [英] PHP Sort array by field?

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

问题描述

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

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

How can I sort an array like: $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'
);

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

So data is displayed in ASC order based off the title field in the array?

推荐答案

使用 usor 明确为此目的而构建.

Use usort which is built explicitly for this purpose.

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

usort($array, "cmp");

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

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