duanlu9816 2017-04-14 23:49
浏览 61
已采纳

根据laravel中的日期对配置变量进行排序

I have a config variable that uses a foreach loop to print out all the objects. Is there a way to sort what prints out based on date? Here is my code for printing out the object. I want to sort it based on $press['date']

@foreach (config('constants.pressMetadata') as $press)
    <div>
        <p id="quote">{{ $press['title'] }}</p>
        <div class="more label"><a id="link" href="{{$press['url']}}">-{{$press['company']}}: {{$press['date']}}</a></div>
        <hr>
    </div>
@endforeach

Here is constants.pressMetadata:

'pressMetadata'=>[
      "AARP" => [
          "id" => 1,
          "company" => "AARP",
          "title" => "Updating Your Résumé for the Digital Age",
          "url" => "http://www.aarp.org/work/job-hunting/info-2016/give-resume-a-digital-reboot.html",
          "date" => "Sep 9, 2016"
      ],
      "Business Insider" => [
          "id" => 2,
          "company" => "Business Insider",
          "title" => "8 things you should always include on your résumé",
          "url" => "http://www.businessinsider.com/what-to-always-include-on-your-resume-2016-1",
          "date" => "Jan 28, 2016"
      ],
      "Morning Journal" => [
          "id" => 3,
          "company" => "Morning Journal",
          "title" => "5 things you missed: Google updates search, Jobscan and more",
          "url" => "http://www.morningjournal.com/article/MJ/20140124/NEWS/140129366",
          "date" => "Jan 24, 2014"
      ],
],
  • 写回答

2条回答 默认 最新

  • duanhuren5581 2017-04-15 04:24
    关注

    You should be able to use Laravel's collections to make this pretty easy. Wrap the call to config() in a call to collect(), and then use the sortBy() method on the collection to sort the records by the strtotime() value of the 'date' key. Use the sortByDesc() method if you want to sort the other way.

    @foreach (collect(config('constants.pressMetadata'))->sortBy(function ($press) { return strtotime($press['date']); }) as $press)
    

    Documentation here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?