6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

連想配列のソート

Last updated at Posted at 2016-10-18

#連想配列のソート
良く忘れてしまうので覚え書き。

  function sort_by_target_date($a, $b) {
        if ($a['target_date'] == $b['target_date']) {
            return 0;
        }
        return ($a['target_date'] < $b['target_date']) ? -1 : 1;
    }
  usort($array, "sort_by_target_date");

日付のこれは昇順ソート。
符号を変えれば降順になる。

$arrayにソートしたい連想配列がある。
その中にtarget_dateがある。

[0]
{
[id]=1
[title]="foo"
[target_date] = "2016-10-10"
}
[1]
{
[id]=2
[title]="bar"
[target_date] = "2016-09-10"
}
[2]
{
[id]=3
[title]="hoge"
[target_date] = "2016-12-10"
}
こういうのをソートできる。 便利。 ちょっと意味が分からないけど…

参考:
php:usort

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?