0

I have this bit of code here and I want to sort the $lessons array by the 'available' field. The array itself contains the lesson ID and the time when the due to start in epoch. I want to sort the time with the first one being the one that is due to start the soonest. I've looked around on the internet but I still don't understand how to use the different sorting functions...

Any help would be great.

    $lessons = array();
    foreach($lessonsArray as $lesson)//for each lesson get the starting time and its lesson id
    {   
        $lessons[] = array( 'id' => $lesson['id'], 'available' => $lesson['available']);            
    }
2

1 Answer 1

0

Try 'uasort()' which receives a callback function as its second parameter. Create the callback function to compare two element arrays in the way you want,

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

then call

uasort($lessons, 'lessonCompare');
Sign up to request clarification or add additional context in comments.

Comments

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.