通过现场PHP排序数组? [英] PHP Sort array by field?

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

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/2545544/how-to-sort-a-multidemensional-array-by-an-inner-key\">how排序由内部密钥结果一期多维数组
  如何排序在PHP中数组的数组?

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

我怎样才能有点像一个数组: $阵列[$ i] ['标题'];

How can I sort an array like: $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 是明确建立用于这一目的。

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天全站免登陆