58980

php sort array with date as key with date format [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/597863/how-to-sort-a-date-array-in-php" dir="ltr" rel="nofollow">How to sort a date array in PHP</a> <span class="question-originals-answer-count"> 4 answers </span> </li> </ul>

I have an array as follow: The first key element is a date with the format dd-mm-yyyy

Array ( [08-12-2015] => Array ( ------------ ) [07-12-2015] => Array ( ------------ ) [09-12-2015] => Array ( ------------ ) )

Is it possible to sort this array on the first key value bij date? So the first element is

Array ( [07-12-2015] => Array ( ------------ ) [08-12-2015] => Array ( ------------ ) [09-12-2015] => Array ( ------------ ) )

Answer1:

Yes, I found the solution:

function order_date($a1,$b1) { $format = 'd-m-Y'; $a = strtotime(date_format(DateTime::createFromFormat($format, $a1), 'Y-m-d H:i:s')); $b = strtotime(date_format(DateTime::createFromFormat($format, $b1), 'Y-m-d H:i:s')); if ($a == $b) { return 0; } else if ($a > $b) { return 1; } else { return -1; } } uksort($array, "order_date");

Tx.

Answer2:

<?php $date= array("08-12-2015"=>"lemon", "09-12-2015"=>"orange", "07-12-2015"=>"banana", "c"=>"apple"); ksort($date); foreach ($date as $key => $val) { echo "$date = $val\n"; } ?>

Something like this might be what you're looking for. Just customize it for your needs. So if you already have an array of these dates you can just pass that into the ksort();

Answer3:

convert your key to strtotime($key) then apply ksort($dates) but it will eliminate the duplicate key.

Recommend

  • Footer occasionally and randomly moving to right side of page as column
  • returning clicked li class in an ul javascript/jquery
  • strtotime() converts a non existing date to another date
  • Show a link in fpdf
  • Omit template arguments when create an instance of template class from another instance of template
  • Tips for creating scalable WPF user control
  • jQuery random blockquote
  • A simple datepicker in VueJS
  • How to set `secure` and `httpOnly` for Plones `__ac` cookie?
  • Excel distinct count with conditions
  • How to search a CSV file with php by checking if a date falls between 2 ranges
  • Get localized short date pattern as String?
  • several dataProvider per one Test in TestNG
  • Parse a date string in a specific locale (not timezone!)
  • Check if a string to interpolate provides expected placeholders
  • How to limit post in wp_query
  • VBA Convert delimiter text file to Excel
  • Websockets service method fails during R startup
  • Rearranging Cells in UITableView Bug & Saving Changes
  • RestKit - RKRequestDelegate does not exist
  • Traverse Array and Display in markup
  • bootstrap to use multiple ng-app
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • Benchmarking RAM performance - UWP and C#
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • python draw pie shapes with colour filled
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Running Map reduces the dimensions of the matrices
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?