排序具有指定键值的数组PHP [重复]

Posted

技术标签:

【中文标题】排序具有指定键值的数组PHP [重复]【英文标题】:Sort An array with its specified key value PHP [duplicate] 【发布时间】:2015-02-14 10:34:02 【问题描述】:

我有一个像

这样的数组
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(日期),还需要使用 noticia 对数组进行排序。我该怎么做?

【问题讨论】:

目前我正在使用这个函数对数组进行排序,但我从来没有得到我想要的结果,我不知道我使用的错误。function sorttitle($a, $b) if ($a- >noticia == $b->noticia) 返回 0;返回 ($a->noticia noticia) ? 1:-1; uasort($this->items, 'sorttitle'); 这里是原始数组 [link]doceysumando.ddns.net:18080/icm-indra2/index.php/videos/videos/… 我想用 DESC 顺序对数组进行排序 此处为空的 [noticia] 值? 父数组的名称? 【参考方案1】:
function cmp($a, $b) 
  return strcmp($a->count, $b->count);

usort($your_array, "cmp");

使用usort()

【讨论】:

什么是 $a 和 $b ?! cmp 函数是如何使用 $a 和 $b 的? @AfghanWiz,请阅读答案中提供的链接文档。 如果我想用 noticia 排序并用 fecha 排序意味着最近的在前.. 那我该怎么办? 我需要一个简单的数组,最近的第一个,并且还按通知排序。你明白我的需要吗?我知道这有点令人困惑,但请帮助我 @ProgrammingStudent,是的,你是对的! :)【参考方案2】:

这使用uasort() 函数按 DESC 顺序按通知字段对数组进行排序:

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

【讨论】:

我需要同时对具有两个键值的数组进行排序,这意味着使用 fecha(date) 最近并且还使用 noticia 对数组进行排序。我该怎么做? 查看更新的答案! 谢谢@AfghanWiz,您的更新回答对我有用。非常感谢 不客气,Prabhash :)

以上是关于排序具有指定键值的数组PHP [重复]的主要内容,如果未能解决你的问题,请参考以下文章

通过具有日期值的单个键对对象数组进行排序

按字母顺序对 NSMutableArray 进行排序[重复]

神奇的算法!看我如何用php实现计数排序

php如何按数组键值排序?

排序数组以及遍历普通对象的键值对以及如何遍历一个不同键值的对象数组

使用php根据特定值对数组进行排序[重复]