对具有指定键值PHP的数组进行排序

时间:2014-12-16 06:55:59

标签: php arrays sorting

我有一个类似

的数组
Array
(
[0] => stdClass Object
    (
        [id] => 3870
        [title] => Ignacio González visita los terrenos donde se ubicará el futuro aeródromo del Suroeste
        [id_galeria] => 3784
        [noticia_id] => 
        [medio] => 
        [programa] => 
        [fecha] => 2013-06-26
        [created] => 0000-00-00 00:00:00
        [video] => /descargas/flv/20130626_aerodromo.flv
        [descargable] => /descargas/mp4/20130626_aerodromo.mp4
        [miniatura] => /images/img/videos/20130626_01.jpg
        [published] => 1
        [checked_out] => 0
        [checked_out_time] => 0000-00-00 00:00:00
        [noticia] => 6020
        [metakey] => 
        [ordering] => 
        [state] => 1
        [created_by] => 
    )
[1] => stdClass Object
    (
        [id] => 3871
        [title] => Ignacio González inaugura la Sede del Instituto Madrileño de Estudios Avanzados de Energía, puntero a nivel mundial
        [id_galeria] => 3785
        [noticia_id] => 
        [medio] => 
        [programa] => 
        [fecha] => 2013-06-26
        [created] => 0000-00-00 00:00:00
        [video] => /descargas/flv/20130626_indea.flv
        [descargable] => /descargas/mp4/20130626_indea.mp4
        [miniatura] => /images/img/videos/20130626_02.jpg
        [published] => 1
        [checked_out] => 0
        [checked_out_time] => 0000-00-00 00:00:00
        [noticia] => 6021
        [metakey] => 
        [ordering] => 
        [state] => 1
        [created_by] => 
    )

[2] => stdClass Object
    (
        [id] => 3879
        [title] => Ignacio González visita los terrenos donde se ubicará el futuro aeródromo del Suroeste
        [id_galeria] => 3784
        [noticia_id] => 
        [medio] => Telemadrid
        [programa] => 
        [fecha] => 2013-06-26
        [created] => 0000-00-00 00:00:00
        [video] => /descargas/flv/20130626_aerodromo_presidencia.flv
        [descargable] => /descargas/mp4/20130626_aerodromo_presidencia.mp4
        [miniatura] => /images/img/videos/20130626_01_tm.jpg
        [published] => 1
        [checked_out] => 0
        [checked_out_time] => 0000-00-00 00:00:00
        [noticia] => 6020
        [metakey] => 
        [ordering] => 
        [state] => 1
        [created_by] => 
    )

[3] => stdClass Object
    (
        [id] => 3867
        [title] => Ignacio González recibe al Real Madrid de Baloncesto tras proclamarse Campeón de Liga
        [id_galeria] => 3778
        [noticia_id] => 
        [medio] => 
        [programa] => 
        [fecha] => 2013-06-25
        [created] => 0000-00-00 00:00:00
        [video] => /descargas/flv/20130620_baloncestorealmadrid.flv
        [descargable] => /descargas/mp4/20130620_baloncestorealmadrid.mp4
        [miniatura] => /images/img/videos/20130620_01_tm.jpg
        [published] => 1
        [checked_out] => 0
        [checked_out_time] => 0000-00-00 00:00:00
        [noticia] => 6010
        [metakey] => 
        [ordering] => 
        [state] => 1
        [created_by] => 
    )

[4] => stdClass Object
    (
        [id] => 3868
        [title] => Ignacio González inaugura el nuevo Centro de Salud de Rivas-Vaciamadrid
        [id_galeria] => 3782
        [noticia_id] => 
        [medio] => 
        [programa] => 
        [fecha] => 2013-06-25
        [created] => 0000-00-00 00:00:00
        [video] => /descargas/flv/20130625_salud.flv
        [descargable] => /descargas/mp4/20130625_salud.mp4
        [miniatura] => /images/img/videos/20130625_01.jpg
        [published] => 1
        [checked_out] => 0
        [checked_out_time] => 0000-00-00 00:00:00
        [noticia] => 6018
        [metakey] => 
        [ordering] => 
        [state] => 1
        [created_by] => 
    )

[5] => stdClass Object
    (
        [id] => 3869
        [title] => Ignacio González entrega los Premios de la Revista Mercados del Vino y la Distribución
        [id_galeria] => 3783
        [noticia_id] => 
        [medio] => 
        [programa] => 
        [fecha] => 2013-06-25
        [created] => 0000-00-00 00:00:00
        [video] => /descargas/flv/20130625_vino.flv
        [descargable] => /descargas/mp4/20130625_vino.mp4
        [miniatura] => /images/img/videos/20130625_02.jpg
        [published] => 1
        [checked_out] => 0
        [checked_out_time] => 0000-00-00 00:00:00
        [noticia] => 6019
        [metakey] => 
        [ordering] => 
        [state] => 1
        [created_by] => 
    )

[6] => stdClass Object
    (
        [id] => 3872
        [title] => Ignacio González inaugura el nuevo Centro de Salud de Rivas-Vaciamadrid
        [id_galeria] => 3782
        [noticia_id] => 
        [medio] => Telemadrid
        [programa] => 
        [fecha] => 2013-06-25
        [created] => 0000-00-00 00:00:00
        [video] => /descargas/flv/20130625_telemadrid_rivascentrosalud.flv
        [descargable] => /descargas/mp4/20130625_telemadrid_rivascentrosalud.mp4
        [miniatura] => /images/img/videos/20130625_01_tm.jpg
        [published] => 1
        [checked_out] => 0
        [checked_out_time] => 0000-00-00 00:00:00
        [noticia] => 6018
        [metakey] => 
        [ordering] => 
        [state] => 1
        [created_by] => 
    )

[7] => stdClass Object
    (
        [id] => 3864
        [title] => Ignacio González visita una residencia donde conocerá una aplicación para confeccionar menús equilibrados para mayores
        [id_galeria] => 3780
        [noticia_id] => 
        [medio] => 
        [programa] => 
        [fecha] => 2013-06-24
        [created] => 0000-00-00 00:00:00
        [video] => /descargas/flv/20130624menu.flv
        [descargable] => /descargas/mp4/20130624menu.mp4
        [miniatura] => /images/img/videos/20130624_01.jpg
        [published] => 1
        [checked_out] => 0
        [checked_out_time] => 0000-00-00 00:00:00
        [noticia] => 6016
        [metakey] => 
        [ordering] => 
        [state] => 1
        [created_by] => 
    )

[8] => stdClass Object
    (
        [id] => 3865
        [title] => Ignacio González asiste a la entrega de la II Edición de los Premios a la Excelencia Educativa del Ayuntamiento de las Rozas
        [id_galeria] => 3781
        [noticia_id] => 
        [medio] => 
        [programa] => 
        [fecha] => 2013-06-24
        [created] => 0000-00-00 00:00:00
        [video] => /descargas/flv/20130624_excelencia.flv
        [descargable] => /descargas/mp4/20130624_excelencia.mp4
        [miniatura] => /images/img/videos/20130624_02.jpg
        [published] => 1
        [checked_out] => 0
        [checked_out_time] => 0000-00-00 00:00:00
        [noticia] => 6017
        [metakey] => 
        [ordering] => 
        [state] => 1
        [created_by] => 
    )

[9] => stdClass Object
    (
        [id] => 3873
        [title] => Ignacio González visita una residencia donde conocerá una aplicación para confeccionar menús equilibrados para mayores
        [id_galeria] => 3780
        [noticia_id] => 
        [medio] => Telemadrid
        [programa] => 
        [fecha] => 2013-06-24
        [created] => 0000-00-00 00:00:00
        [video] => /descargas/flv/20130624_telemadrid_menusmayores.flv
        [descargable] => /descargas/mp4/20130624_telemadrid_menusmayores.mp4
        [miniatura] => /images/img/videos/20130624_01_tm.jpg
        [published] => 1
        [checked_out] => 0
        [checked_out_time] => 0000-00-00 00:00:00
        [noticia] => 6016
        [metakey] => 
        [ordering] => 
        [state] => 1
        [created_by] => 
    )
)

我想用一个名为[noticia]的键值对数组进行排序。我已经尝试了很多但是我没有在不改变其索引的情况下使用此键值进行排序。

我需要对具有两个键值的数组进行排序,同时表示最近的fecha(date)以及带有noticia的排序数组。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

function cmp($a, $b) {
  return strcmp($a->count, $b->count);
}
usort($your_array, "cmp");

使用usort()

答案 1 :(得分:0)

这使用uasort()函数在DESC顺序中按照noticia字段对数组进行排序:

function fn($a, $b) {
     if ($a['fetcha'] == $b['fetcha']) {
        if($a['noticia'] == $b['noticia']) {
           return 0;
        }
        return ($a['noticia'] > $b['noticia']) ? -1 : 1;
    }
    return ($a['fetcha'] > $b['fetcha']) ? -1 : 1;
}
uasort($arrayName, "fn");

echo "<pre>";print_r($arrayName);