dongzang5815 2013-09-18 18:43
浏览 20
已采纳

根据日期组织 - 最简单/优雅的方式

Ok, I've got some code that creates an array, and then iterates over that array, and from one of the items, gets an id, and a title.

What I WOULD like, is to be able to organize based on date.

Now as you can see, I already grab the date per item, however there's no real easy way to organize based on it that I can see. I suppose I could pull the entity id, check the date on it, and put that in order, and then generate the additional data after that - would that be the simplest method?

$multi_reg_id = multi_reg_get_id();
  foreach ($multi_reg_id as $regid) {
    $node = node_load($regid->entity_id);
    $title = $node->title;
    $date = $node->field_event_date[LANGUAGE_NONE][0]['value'];
    $entity_id = (string)$regid->entity_id;

    $dt = new DateTime($date);
    $date = $dt->format('l, F d, Y - g:ia');
    $checkbox[$entity_id] = $title . " | " . $date;
        }


return $checkbox;
  • 写回答

1条回答 默认 最新

  • douxue4242 2013-09-18 18:55
    关注

    You can use the timestamps as an array key, then use ksort to sort. Finally, flatten the multidimmension with call_user_func_array and array_merge:

    $multi_reg_id = multi_reg_get_id();
    
    $output = array();
    foreach ($multi_reg_id as $regid) {
        $node = node_load($regid->entity_id);
        $title = $node->title;
        $date = $node->field_event_date[LANGUAGE_NONE][0]['value'];
        $entity_id = (string)$regid->entity_id;
    
        $dt = new DateTime($date);
        $date = $dt->format('l, F d, Y - g:ia');
    
        if (!array_key_exists($dt->getTimestamp(), $output))
            $output[$dt->getTimestamp()] = array();
        $output[$dt->getTimestamp()][$entity_id] = $title . " | " . $date;
    }
    
    ksort($output);
    $output = call_user_func_array('array_merge', $output);
    
    return $output;
    

    Documentation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制