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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:19:40+00:00 2026-05-15T22:19:40+00:00

I have an array that I wish to sort, it contains images with file

  • 0

I have an array that I wish to sort, it contains images with file extensions, the filename is numeric and the file extension is obviously a string.

$files = array();
$files[] = '4.jpg';
$files[] = '14.jpg';
$files[] = '1.jpg';
$files[] = '44.jpg';

If i use sort() then I end up with the following:

sort($files);
print_r($files);

Array
(
    [0] => 1.jpg
    [1] => 14.jpg
    [2] => 4.jpg
    [3] => 44.jpg
)

What i actually want is:

Array
(
    [0] => 1.jpg
    [1] => 4.jpg
    [2] => 14.jpg
    [3] => 44.jpg
)

Is this possible?

  • 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-15T22:19:41+00:00Added an answer on May 15, 2026 at 10:19 pm

    The quickest an easiest way of doing this is with natsort() – http://php.net/manual/en/function.natsort.php

    e.g:

    natsort($files);
    print_r($files);
    
    Array
    (
        [0] => 1.jpg
        [1] => 4.jpg
        [2] => 14.jpg
        [3] => 44.jpg
    )
    

    This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a “natural ordering”.

    you can also use sort_flags option in the sort() function

    sort($files, SORT_NUMERIC);
    print_r($files);
    
    Array
    (
        [0] => 1.jpg
        [1] => 4.jpg
        [2] => 14.jpg
        [3] => 44.jpg
    )
    

    The optional second parameter sort_flags may be used to modify the sorting behavior using these values:

    Sorting type flags:

    SORT_REGULAR – compare items normally (don’t change types)

    SORT_NUMERIC – compare items numerically

    SORT_STRING – compare items as strings

    SORT_LOCALE_STRING – compare items as strings, based on the current locale. Added in PHP 4.4.0 and 5.0.2, it uses the system locale, which can be changed using setlocale().

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

Sidebar

Related Questions

I have an array that contains a list of vertices which I copy to
I have an array that contains the RGB colour values for each pixel in
I have an array that I want to sort based on the value of
I have an array of strings and I wish to find out if that
So I have an array of indexes of characters in a string that I
I wish to have a large array of QColors that many classes will share
I have an array that looks like: $fields = array( f1 => array(test), f2
I have an array that has keys and values. For eg: Array ( [name]
I have an array that looks like $numbers = array('first', 'second', 'third'); I want
I have an array that is a member of a structure: $self->{myArray} = [value1,

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.