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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:02:33+00:00 2026-05-31T12:02:33+00:00

I have 4 arrays and use array_multisort to sort them all at the same

  • 0

I have 4 arrays and use array_multisort to sort them all at the same time relative to one another.
Problem is, in the first array, there can be empty values and I want to put them at the end, not the beginning.

Example : http://codepad.org/V6TjCsS5

Is there a way to:

  • Pass a custom function to array_multisort
    or
  • Sort the first array with a custom function then use the result order to sort the other arrays
    or
  • Use a certain argument with array_multisort to achieve what I want

Thank you very much

  • 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-31T12:02:34+00:00Added an answer on May 31, 2026 at 12:02 pm

    Unfortunately none of the approaches your propose is possible, which means you have to take another step back and look for alternatives. I am assuming you want a normal ascending sort, with the explicit exception that empty elements (which you be “smallest”) need to be considered as “largest”.

    Option 1: Manually rearrange elements after sorting

    Do your array_multisort as usual, and then make the modifications you require:

    // $arr1, $arr2 etc have been sorted with array_multisort
    while(reset($arr1) == '') {
        $k = key($arr1);
        unset($arr1[$k]); // remove empty element from beginning of array
        $arr1[$k] = ''; // add it to end of array
        // and now do the same for $arr2
        $v = reset($arr2);
        $k = key($arr2);
        unset($arr2[$k]);
        $arr2[$k] = $v;
        // the same for $arr3, etc
    }
    

    You can pull out part of the code in a function to make this prettier:

    function shift_and_push(&$arr) {
        $v = reset($arr);
        $k = key($arr);
        unset($arr[$k]);
        $arr[$k] = $v;
    }
    

    Option 2: Condense everything inside one array so you can use usort

    The idea here is to pull all your arrays into one so you can specify the comparison function by using usort:

    $allArrays = array_map(function() { return func_get_args(); },
                           $array1, $array2 /* , as many arrays as you want */);
    

    You can now sort:

    // writing this as a free function so that it looks presentable
    function cmp($row1, $row2) {
        // $row1[0] is the item in your first array, etc
        if($row1[0] == $row2[0]) {
            return 0;
        }
        else if($row1[0] == '') {
            return 1;
        }
        else if($row2[0] == '') {
            return -1;
        }
    
        return $row1[0] < $row2[0] ? -1 : 1;
    }
    
    usort($allArrays, "cmp");
    

    At this point you are left with an array, each element (row) of which is an array. The first elements of each are what was originally inside $array1, second elements are what was in $array2, etc. By placing those elements inside “rows”, we have managed to keep the sort order among all your original arrays synchronized.

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

Sidebar

Related Questions

I have a situation where I have two arrays and I use one array(A)
I have an array of strings plus one additional string. I want to use
I have an array of different type objects and I use a BinaryWriter to
I have an array of strings that I want to use for button titles
I have set an array in my config file that I use global in
I have the following array: a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] I use it for some visual
I use a lot of lists and arrays but I have yet to come
I have an SHA-1 byte array that I would like to use in a
A PHP array can have arrays for its elements. And those arrays can have
I don't know why I have to use Array.toString in the split(String reges) method.

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.