Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:16:13+00:00 2026-05-28T19:16:13+00:00

I am fairly new to php and would love to hear your thoughts on

  • 0

I am fairly new to php and would love to hear your thoughts on this function I just wrote.
The idea is to sort an array of files chronologically from oldest to newest. Take a look and let me know if you think it can be improved at all:

function sortByDate($filearray){
    $OAR = array();
    foreach($filearray as $item){
        $OAR[filemtime($item)] = $item;
        }
    ksort($OAR);
    return explode(" * ", implode(" * ", $OAR ));
    }

Thanks for your time!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:16:14+00:00Added an answer on May 28, 2026 at 7:16 pm

    If more than one file has the same mtime, it will overwrite that file from your $OAR array. Instead store each value as an array of files to solve that issue.

    And this statement return explode(" * ", implode(" * ", $OAR )); seems like it can just be replace with return array_values($OAR); With the above adjustment I mentioned, you may have to loop over the $OAR array and push each set of values to a new array and return that instead.

    EDIT: here is an example.

    function sortByDate($filearray){
        $OAR = array();
        $sorted = array();
    
        foreach($filearray as $item){
            $mtime = filemtime($item);
            if (!isset($OAR[$mtime])) $OAR[$mtime] = array();
    
            $OAR[$mtime][] = $item;  // push $item to the array for this mtime
        }
    
        ksort($OAR);
    
        foreach($OAR as $key => $values) {
            if (sizeof($values) > 1) sort($values); // sort by name
            foreach($values as $file) {
                $return[] = $file;
            }
        }
    
        return $return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to PHP and MongoDB so it would be really helpful if
I am fairly new to php and was wondering how I would go about
I'm fairly new to php and was wondering how do I validate the array
I am fairly new to PHP and Codeigniter so im sure this will be
I'm fairly new to PHP so am not sure how to do this. I
I'm fairly new to PHP, and I just started using NetBeans to develop my
i'm fairly new to PHP and can't seem to figure this one out. i've
I am fairly new to php and have not figured out how I would
Notes: I'm still fairly new to php This login form seems to work perfectly
To put it simply i am a fairly new PHP coder and i was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.