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

  • Home
  • SEARCH
  • 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 734943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:25:54+00:00 2026-05-14T07:25:54+00:00

I am after a function which will let me sort a bunch of filenames

  • 0

I am after a function which will let me sort a bunch of filenames over 4 arrays.
However, the a file must always end up in the same array – even if the number of files change.

Eg if I have files
myfile.html
anotherfile.html
morefiles.html
test.html

and my arrays
array1, array2, array3, array4

If I run this function then
array1 might get myfile.html and anotherfile.html

If I run it again and add some more files (or less files, like not pass anotherfile.html) then I would still expect array1 to get myfile.html

So just need some way to hash the filename which I can then use to point to a certain array, so not a random or one that checks how many files are in each array needs to be consistent.

  • 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-14T07:25:54+00:00Added an answer on May 14, 2026 at 7:25 am

    There are many different ways to solve a task like this, what is below is a very basic introduction to the topic. If it is useful, great otherwise I hope it at least gives an idea what where you might want to go (or not!).

    This example simple takes a hash of the filename (in this case MD5 just because you’re probably familiar with it). The sscanf just gets the first character of the hash and turns it into a number between 0 and 15 (since md5() returns a hexadecimal number). Since we only want to distribute between four arrays, the modulus operator (%) is used so that $num will always result in 0, 1, 2 or 3 which is then used as an array key (c.f. your $array1, $array2, etc.).

    $files = array('a.html','b.html','c.html','d.html');
    $arrays = 4;
    $array = array_fill(0, $arrays, array());
    // For each file name put it into the appropriate slot in $array
    foreach ($files as $filename) {
        sscanf(md5($filename), '%1x', $hex);
        $key = $hex % $arrays;
        $array[$key][] = $filename;
    }
    // See what happened
    var_dump($array);
    

    For this particular example, the resulting array (which you can push into your separate variables if you like) has the following structure:

    $array = array(
        0 => array()
        1 => array('c.html')
        2 => array('d.html')
        3 => array('a.html', 'b.html')
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTable inside a JScrollPane. After calling my function which should populate
So I have this code which is called in a print_info() function after a
in my application i am using xmlhttp, which is called by a function after
Which is the correct function to call to exit the child process after os.fork()
Inside my Controller i have function that runs after user clicks on item, which
To debug our Android code we have put System.out.println(string) which will let us know
Is there a Chrome extension post install hook/API function that will let me perform
I am writing a module, let's call it base , which will show a
Possible Duplicate: What is useful about this C syntax? C variable declarations after function
I wish to execute a javascript function after asp.net postback with out using ajax.

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.