3

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"
      ],
],
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Aaron
  • 4,380
  • 19
  • 85
  • 141
  • Possible duplicate of [PHP Sort a multidimensional array by element containing date](http://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date) – mickmackusa Apr 15 '17 at 01:11
  • you devalue SO as a resource when you do not search existing answer before posting a question. Look at how many answers would have helped you: http://stackoverflow.com/search?q=how+to+sort+multidimensional+array+by+date – mickmackusa Apr 15 '17 at 01:12
  • downvoted because didn't show any effort to research and self-solve – mickmackusa Apr 15 '17 at 01:20

2 Answers2

1

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.

patricus
  • 59,488
  • 15
  • 143
  • 145
  • Thanks, I knew there was a laravel way of doing this, I just could not find any documentation that did not point me to vanilla php – Aaron Apr 17 '17 at 18:04
0

You can use the usort function of PHP.

The following code is taken from the PHP manual and changed to reflect your needs

function cmp($a, $b)
{
    if (strtotime($a['date']) == strtotime($b['date'])) {
        return 0;
    }
    return (strtotime($a['date']) < strtotime($b['date'])) ? -1 : 1;
}

usort(config('constants.pressMetadata'), "cmp");
Simone Cabrino
  • 901
  • 9
  • 24
  • If you are only going to copy-paste manual code, just leave a link as a comment. Otherwise please edit your answer to suit this exact question. This is a multi-dimensional array. – mickmackusa Apr 14 '17 at 23:58
  • This code does not deliver the expected result. You did not test before posting. Do the work if you are going to provide an answer. Otherwise, delete your answer and move on. – mickmackusa Apr 15 '17 at 00:05
  • @mickmackusa here is your tested and fully running script! – Simone Cabrino Apr 15 '17 at 01:01
  • Well done, next time do the right thing without being told. – mickmackusa Apr 15 '17 at 01:07
  • @Aaron This code seems to be the correct answer (probably unbeatable). If this satisfies you, please award the green tick so that your question is deemed resolved. – mickmackusa Apr 15 '17 at 01:08