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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:06:34+00:00 2026-05-24T17:06:34+00:00

I am trying to sort a multi dimensional array, and have one value always

  • 0

I am trying to sort a multi dimensional array, and have one value always at the end of the array. The array should be sorted by ‘unitText’ (dont care how unitID is sorted), but always have “Last” as the last value in the array. I’ve almost got it, but not quite there.

<?php

function cmp($a, $b)
{
    /*
    $a = preg_replace('@^(Last) @', '', $a);
    $b = preg_replace('@^(Last) @', '', $b);
    return strcasecmp($a, $b);
    */

    if(strtolower(substr($a['unitText'],0,4))=="last") return strnatcmp($a['unitText'],9999);
    else if(strtolower(substr($b['unitText'],0,4))=="last") return strnatcmp(9999,$b['unitText']);
    else return strnatcmp($a, $b);

    //return strnatcmp($a['unitText'], $b['unitText']);

    //return end($a) > end($b);

}

$unit = array(
    array("unitID"=>80, "unitText"=>"Q701"),
    array("unitID"=>30, "unitText"=>"H568"),
    array("unitID"=>25, "unitText"=>"Last"),
    array("unitID"=>40, "unitText"=>"Z255"),
    array("unitID"=>20, "unitText"=>"A459")
);

echo "<pre>";
print_r($unit);
echo "</pre>";

echo "<hr/>";

//uksort($unit['unitText'], "cmp");
//array_multisort($unit['unitText'], SORT_DESC, $unit['unitID'], SORT_ASC, $unit);
usort($unit, 'cmp');

echo "<pre>";
print_r($unit);
echo "</pre>";

?>

Should end up with:

Array
(
    [0] => Array
        (
            [unitID] => 20
            [unitText] => A459
        )

    [1] => Array
        (
            [unitID] => 30
            [unitText] => H568
        )

    [2] => Array
        (
            [unitID] => 80
            [unitText] => Q701
        )

    [3] => Array
        (
            [unitID] => 40
            [unitText] => Z255
        )

    [4] => Array
        (
            [unitID] => 25
            [unitText] => Last
        )

)

What am I doing wrong?

  • 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-24T17:06:35+00:00Added an answer on May 24, 2026 at 5:06 pm

    Your problem is with this line:

    else return strnatcmp($a, $b);
    

    Remember that $a and $b are both arrays, but strnatcmp() compares strings. When this function is called, your two arrays will be implicitly cast to strings, which will both have the value of Array, so they will return as equal and will not be sorted.

    You should be comparing the unitText value:

    else return strnatcmp($a['unitText'], $b['unitText'])
    

    The fact that you didn’t see this tells me that you don’t have your error_reporting level set high enough in development, since that implicit cast issues an E_NOTICE when it occurs. In development, you should always have error_reporting(E_ALL | E_STRICT); set (either at the entry point for your code, or in your php.ini, etc.) so that any little issues get immediately flagged for you to fix.

    Also, there is no reason why you should be calling strnatcmp() at all when one of the values is ‘Last’, because you already know that the value should be last. Just return 1 or -1 (depending on which contains ‘Last’) and be done with it.

    Finally, you don’t need all the else conditions in the code. Since all paths issue a return, you can assume that anything that comes after an if block is only executed if the comparison fails:

    if (strtolower(substr($a['unitText'],0,4))=="last") {
      return 1;
    }
    
    if strtolower(substr($b['unitText'],0,4))=="last") {
      return -1;
    }
    
    return strnatcmp($a['unitText'], $b['unitText']);
    

    In the above, the second if only executes if we didn’t find ‘Last’ within $a, since the function would have already ended if it had. Similarly, the final return statement (with the strnatcmp() call) only executes if neither of the above if conditions passed, because either one of them would have returned a value and ended the function.

    It’s a little thing, but nesting a bunch of unneeded if and else blocks makes the code less readable.

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

Sidebar

Related Questions

I'm trying to sort a multidimensional array by multiple keys, and I have no
I have a multi dimensional array called soldier: Array ( [0] => Array (
I'm trying to sort a multidimensional array in objective-c i know that i can
Trying to sort this array of objects according to (1) depth and (2) weight,
I'm trying to sort any array with array_multisort() and everything is working great. However,
Trying to sort the array below by memnum in ascending order, and I'm a
I am trying to sort an array that contains numbers that range in substantial
Trying to sort an array by writing my own sort method using recursion (Pine's
I am having a hard time trying recursively sort a multidimensional array on its
Trying to sort an array in PHP that is being populated from a CSV.

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.