簡體   English   中英

如何按日期對此數組進行排序?

[英]How to sort this array by date?

我正在嘗試對這種數組進行排序:

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

你有這個想法嗎?

您需要使用uasort編寫一個函數來為您排序數組。

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

基本上你編寫自己的比較函數並將其作為回調傳遞給uasort方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM