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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:00:58+00:00 2026-06-01T02:00:58+00:00

In PHP, how might one sort an array by the last couple of characters

  • 0

In PHP, how might one sort an array by the last couple of characters of its values? Take, for example, the following array:

$donuts[0] = "Chocolate 02";
$donuts[1] = "Jelly 16";
$donuts[2] = "Glazed 01";
$donuts[3] = "Frosted 12";

After the sort, the array would look like this (note the order based on the last two characters of each value… also note the rewritten indexes):

$donuts[0] = "Glazed 01";
$donuts[1] = "Chocolate 02";
$donuts[2] = "Frosted 12";
$donuts[3] = "Jelly 16";

I can’t seem to find a built-in function that can do this and have been racking my brain for the simplest and most efficient way to get this accomplished. Help! And thanks!

  • 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-01T02:01:00+00:00Added an answer on June 1, 2026 at 2:01 am

    This should do the trick:

    header('Content-Type: Text/Plain');
    $donuts[0] = "Chocolate 02";
    $donuts[1] = "Jelly 16";
    $donuts[2] = "Glazed 01";
    $donuts[3] = "Frosted 12";
    
    print_r($donuts);
    
    usort($donuts, function ($a, $b){
        return substr($b, -2) - substr($a, -2);
    });
    
    print_r($donuts);
    

    NOTE

    1. To convert from highest to smallest:

      return substr($b, -2) - substr($a, -2);
      
    2. This answer make an assumption that the last 2 characters is to be used.

    UPDATE

    To make it work on PHP version 5.2, change the return part to:

    return substr($b, strlen($b) - 2) - substr($a, strlen($a) - 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several php scripts that have the following structures: $count = $_GET['count']; $sort
Introduction I have some sort of values that I might want to access several
Typically I use E_ALL to see anything that PHP might say about my code
I am about to write a tender. The solution might be a PHP based
I know the title isn't very clear. I'm new to PHP, so there might
PHP's explode function returns an array of strings split on some provided substring. It
PHP stores its session information on the file system of the host of the
So, I have an array of objects that I want to sort like so:
I dunno guys, this is a really weird one, but I might just be
I've got a PHP script in the works that is a job worker; its

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.