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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置