0

Trying to sort the array below by "end" without the use of usort function, is there a simple way to do so?

$feature_event = array(
    array(
        "title"     => "Classic",
        "date"      => "October 28 - 30 2016",
        "end"       => strtotime("October 30, 2016")
    ),
    array(
        "title"     => "Ski Weekend",
        "date"      => "February 5-8, 2017",
        "end"       => strtotime("February 8, 2017")
    ),
    array(
        "title"     => "NBA Weekend",
        "date"      => "February 17-19, 2017",
        "end"       => strtotime("February 19, 2017")
    ),
    array(
        "title"     => "Fiesta",
        "date"      => "May 26-28, 2017",
        "end"       => strtotime("May 28, 2017")
    )

I'm outputting the result via a foreach loop but want to limit to 3 results.

<?php foreach( array_slice($feature_event, 0, 3) as $event): ?>
...
<?php endforeach; ?> 

Small explanation: I'm trying to display images that are associated with an event. Each event has a specific end date. Once the end date is past, display the next image in the loop...but I want the array sorted by the end date. I understand a usort function would do the trick but I'm not that advance in coding. Is there a simple way of accomplishing sorting by "end" or would I need to do a usort. If needing a usort would I still need the forloop? Plz excuse my dumb question.

UPDATE

This array multisort solution

array_multisort(array_column($feature_event, 'end'), $feature_event);

sorts the way I'm needing it to but I would like to output the data based off the current date...I can't seem to create a conditional that works correctly...

within my forloop I have this conditional:

<?php 
date_default_timezone_set("America/Chicago"); 
$now = strtotime("now");
?>

<?php if($now <= $event["end"]) :?>
    ... code ...
<?php endif;?>

but it is only outputting 2 events....any ideas on what I'm doing wrong. This if is inside the foreachloop above.

3
  • 1
    is this from a DB? why not do that limiting and sorting from there, there no more array_slice and usort Commented Jul 25, 2016 at 3:05
  • This is static data for a small website. Is it a way to sort and limit in php based on the key of a subarray that doesnt involve using usort? As I said I would prefer a simple solution but if there is a better option a little more advance then I wouldnt be opposed to it. Commented Jul 25, 2016 at 3:14
  • Edited answer, I mis-read the strtotime() part. Commented Jul 25, 2016 at 14:43

1 Answer 1

1

Since the end values are timestamps, just extract those into an array and sort that, sorting the original array:

PHP < 5.5.0:

array_multisort(
    array_map(function($v) { return $v['end']; }, $feature_event), $feature_event);

PHP >= 5.5.0:

array_multisort(array_column($feature_event, 'end'), $feature_event);

Or use SORT_DESC as the third parameter depending on which way you want to sort.

Then use your array_slice() to get the first or last 3 and loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice, this sorts correctly. I did a print_r of the array and checked. My only problem now is creating the correct conditional to output the data from the loop based off the current date and the end dates...so basically, the 3 most recent events that has not exceeded the end date....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.