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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:46:57+00:00 2026-05-30T02:46:57+00:00

Firstly, YES, this is a duplicate issue that’s been asked 100 times on here,

  • 0

Firstly, YES, this is a duplicate issue that’s been asked 100 times on here, and I’ve read through many of the posts, but the examples given aren’t working 100% for me and I can’t figure out where I’m going wrong.

I want to sort an array like this by the ‘distance’ key, and I want the sort to be “expanded” across the entire array so it reorders all keys in the array. In essence, just like you’d get if you sorted a column in a spreadsheet and expanded the sort across all columns.

Here’s the sample array:

$locations = array ( 
[phone] => Array ( 
    [0] => 555-123-1234 
    [1] => 555-456-4321 
    [2] => 555-789-1122 
    [3] => 555-321-1111 ) 
[address] => Array ( 
    [0] => 123 Main Street BigCity, NY 12345
    [1] => 456 Maple Street Somewhere, UT 87654
    [2] => 999 1st Ave MyTown, VA 23456
    [3] => 321 2nd Ave MyTown, VA 23456 ) 
[distance] => Array ( 
    [0] => 24.56
    [1] => 13.05
    [2] => 23.99
    [3] => 1.75 ) 
[name] => Array ( 
    [0] => First Location
    [1] => Second Location
    [2] => Third Location
    [3] => Fourth Location ) 
)

I’m trying to sort the array by ‘distance’, and I’ve tried using both the array_multisort() function and the usort() function …but only “multisort” gives me nearly usable results by sorting the distances only and not sorting the entire array according to distance.

My function:

array_multisort($locations['distance'], SORT_NUMERIC);

I then run a “for” loop to traverse the array and spit out the values by key. The result is the distances are ordered but the Location names, addresses, etc. don’t get reordered.

What am I missing here??

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-05-30T02:46:59+00:00Added an answer on May 30, 2026 at 2:46 am

    Your array is arranged in columns, whereas array_multisort() when used in this way expects it to be arranged in rows.

    It should be in the structure that you would get if you did:

    $rows = array();
    while ($row = $result->fetch_assoc()) $rows[] = $row;
    

    …with a MySQLi result object.

    You can reformat your existing array into this structure quite easily:

    $locationsAsRows = array();
    for ($i = 0; isset($locations['phone'][$i]); $i++) {
      $locationsAsRows[$i] = array (
        'phone' => $locations['phone'][$i],
        'address' => $locations['address'][$i],
        'distance' => $locations['distance'][$i],
        'name' => $locations['name'][$i]
      );
    }
    var_dump($locationsAsRows);
    

    Now, when you do:

    array_multisort($locations['distance'], SORT_NUMERIC, $locationsAsRows);
    var_dump($locationsAsRows);
    

    …you can see that the data has been sorted. Note that not only did I pass the column data you wanted to sort by, I also passed the whole “row formatted” $locationsAsRows array as the last argument. This is passed by reference, so $locationsAsRows itself will be the sorted data after you have called array_multisort().

    Now let’s say that two of the distance values are the same – we need to supply a second priority search column. So next, we could sort by address, and we can do that like so:

    array_multisort($locations['distance'], SORT_NUMERIC, $locations['address'], SORT_ASC, $locationsAsRows);
    

    See it working and have a play around with it…

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

Sidebar

Related Questions

Firstly I know that there are many question and solutions to correct thread marshalling
Firstly, I'm not using rails. This is vanilla ruby application. I've read about packaging
Firstly, I just want to say that the gmaps4rails gem has been fantastic. Easy
Firstly, I'm in C# here so that's the flavor of RegEx I'm dealing with.
There is one issue that I've been struggling with and I can't seem to
Firstly, I've read through the posts with matching names, and I've tried integrating some
Firstly, let me say that I've been searching the Internet, rewriting scripts and I
Firstly, here's my script: #!/usr/bin/python import sys, os sys.path.append('/home/username/python') sys.path.append(/home/username/python/flup) sys.path.append(/home/username/python/django) # more path
Firstly: I am totally a newbie for this kind of work. I have a
Firstly, I was wondering if there was some kind of built in function that

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.