【问题标题】:How to sort this array by date?如何按日期对该数组进行排序?
【发布时间】:2011-04-14 08:14:04
【问题描述】:

我正在尝试对此类数组进行排序:

array (
  0 => 
  array (
    'id_ouverture' => 5,
    'debut' => '2011-04-25 08:00:00',
    'fin' => '2011-04-25 20:00:00',
    'id_salle' => 
    array(
       'id' => '7',
       'nom' => 'BLABLA',
       'id_type_salle' => '3',
       'visible' => 1,          
    ),
  ),
  1 => 
  array (
    'id_ouverture' => 6,
    'debut' => '2011-04-18 08:00:00',
    'fin' => '2011-04-18 10:45:00',
    'id_salle' => 
    array(
       'id' => '7',
       'nom' => 'BLABLA',
       'id_type_salle' => '3',
       'visible' => 1,           
    ),
  ),
  2 => 
  array (
    'id_ouverture' => 7,
    'debut' => '2011-05-02 08:00:00',
    'fin' => '2011-05-02 10:45:00',
    'id_salle' => 
    array(
       'id' => '7',
       'nom' => 'BLABLA',
       'id_type_salle' => '3',
       'visible' => 1,
    ),
  ),
  3 => 
  array (
    'id_ouverture' => 8,
    'debut' => '2011-05-09 08:00:00',
    'fin' => '2011-05-09 10:45:00',
    'id_salle' => 
    array(
       'id' => '7',
       'nom' => 'BLABLA',
       'id_type_salle' => '3',
       'visible' => 1,           
    ),
  ));

我需要根据这个键对这个数组进行排序:array[$i]['debut'] 按升序排列。

结果必须是:

array (
  1 => 
  array (
    'id_ouverture' => 6,
    'debut' => '2011-04-18 08:00:00',
    'fin' => '2011-04-18 10:45:00',
    'id_salle' => 
    array(
       'id' => '7',
       'nom' => 'BLABLA',
       'id_type_salle' => '3',
       'visible' => 1,           
    ),
  ),
  0 => 
  array (
    'id_ouverture' => 5,
    'debut' => '2011-04-25 08:00:00',
    'fin' => '2011-04-25 20:00:00',
    'id_salle' => 
    array(
       'id' => '7',
       'nom' => 'BLABLA',
       'id_type_salle' => '3',
       'visible' => 1,          
    ),
  ),

  2 => 
  array (
    'id_ouverture' => 7,
    'debut' => '2011-05-02 08:00:00',
    'fin' => '2011-05-02 10:45:00',
    'id_salle' => 
    array(
       'id' => '7',
       'nom' => 'BLABLA',
       'id_type_salle' => '3',
       'visible' => 1,
    ),
  ),
  3 => 
  array (
    'id_ouverture' => 8,
    'debut' => '2011-05-09 08:00:00',
    'fin' => '2011-05-09 10:45:00',
    'id_salle' => 
    array(
       'id' => '7',
       'nom' => 'BLABLA',
       'id_type_salle' => '3',
       'visible' => 1,           
    ),
  ));

你有想法吗?

【问题讨论】:

    标签: php arrays algorithm multidimensional-array


    【解决方案1】:

    您需要使用uasort 编写一个函数来为您排序数组。

    http://php.net/manual/en/function.uasort.php

    基本上你编写自己的比较函数并将其作为回调传递给uasort 方法

    【讨论】:

      猜你喜欢
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 2021-02-23
      相关资源
      最近更新 更多