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 8073311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:18:01+00:00 2026-06-05T14:18:01+00:00

I have is an array of markdown files like this: $mdfiles = glob(content/*.txt, GLOB_NOSORT);

  • 0

I have is an array of markdown files like this:

$mdfiles = glob("content/*.txt", GLOB_NOSORT);

I want to sort the files by a certain line inside each of them.

An example file is:

File
====
line-one:
date: [number-to-sort]

The array of files is then sorted by [number-to-sort] in each file, which can be accessed by:

$file_array = file($mdfiles[*], FILE_IGNORE_NEW_LINES)
substr($file_array[*], 6);

Finally I would like to strip each content/ and .md from the array key values.

  • 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-06-05T14:18:03+00:00Added an answer on June 5, 2026 at 2:18 pm

    The code looked a lot smaller in my mind, but the resulting code is just three lines 🙂

    $files = glob('content/*.txt', GLOB_NOSORT);
    // sort the file array by date; see below
    usort($files, 'by_file_date');
    // strip the filename
    $files = array_map('strip_filename', $files);
    

    The 'by_file_date' function is declared later and basically uses the get_date function internally to perform the date “pulling” from the files. I’ve used preg_match based on the form you’ve shown to find the date value; I’ve assumed that date is an integer (i.e. sequence of digits). If not, let me know.

    // pull date value from the file
    // @todo this function can be optimized by keeping a static array of
    //   files that have already been processed
    function get_date($f)
    {
        // match the date portion; i'm assuming it's an integer number
        if (preg_match('/^date:\s*(\d+)/', file_get_contents($f), $matches)) {
            return (int)$matches[1];
        }
        return 0;
    }
    
    function by_file_date($a, $b)
    {
        // sort by date ASC
        return get_date($a) - get_date($b);
    }
    

    Finally, you need to strip the filename; Assuming you just want the file name and not the directory:

    function strip_filename($f)
    {
        // strip the directory portion
        return basename($f);
    }
    

    Not sure where .md came from, so you have to let me know on that one 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have array like this: $test = array(20, 30, 40); And I want to
I have array like this: $test = array(20, 30, 40); And I want to
I have Array of time string like 10:10:12,12:10:34,05:23:11 this. I want to calculate less(min)
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have array that looks like this, Array ( [email_address] => Array ( [0]
I have array like this: array( 'person0' => array( 'name'=>'name0','address'=>'address0' ), 'person1' => array(
I have array like this: array( [0] => Array ( [q_id] => 314 [answer_type]
I have array like this var Arr = [ 'h78em', 'w145px', 'w13px' ] I
I have array $arr=array(a=>array(b=>123)) and I need to do with it something like this:

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.