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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:05:35+00:00 2026-06-15T10:05:35+00:00

I have a total of three arrays Array 1 array1 = Array( [0] =>

  • 0

I have a total of three arrays

Array 1

array1 = Array( 
    [0] => Array(
        [name] => John 
        [city] => San Francisco 
        [state] => CA 
    ) 
    [1] => Array(
        [name] => Smith 
        [city] => Atlanta 
        [state] => GA 
    ) 
    [2] => Array(
        [name] => Peter 
        [city] => New York 
        [state] => NY 
    ) 
    [3] => Array(
        [name] => Mary 
        [city] => San Jose 
        [state] => CA 
    ) 
)

Array 2 (sorted on average age)

array2 = Array(
    [0] => CA 
    [1] => NY 
    [2] => GA
) 

Array 3 (sorted based on population)

array3 = Array(
    [0] => New York 
    [1] => San Francisco 
    [2] => Atlanta 
    [3] => San Jose
)

How do I sort array 1 based on array 2 first and then by array 3. I want the following output:

Sorted Array 1

Array
(
    [0] => Array
        (
            [name] => John
            [city] => San Francisco
            [state] => CA
        )

    [1] => Array
        (
            [name] => Mary
            [city] => San Jose
            [state] => CA
        )

    [2] => Array
        (
            [name] => Peter
            [city] => New York
            [state] => NY
        )

    [3] => Array
        (
            [name] => Smith
            [city] => Atlanta
            [state] => GA
        )

)

I tried using array_multisort for achieving this, but it’s not working, e.g.

array_multisort($array2,$array3,$array1)

Any help is highly appreciated.

  • 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-15T10:05:36+00:00Added an answer on June 15, 2026 at 10:05 am

    Here’s one way to sort $array1 by $array2 and $array3 to produce the results you want:

    <?php
    
    $array1 = array(
        array('name' => 'John',  'city' => 'San Francisco', 'state' => 'CA'),
        array('name' => 'Smith', 'city' => 'Atlanta',       'state' => 'GA'),
        array('name' => 'Peter', 'city' => 'New York',      'state' => 'NY'),
        array('name' => 'Mary',  'city' => 'San Jose',      'state' => 'CA'),
    );
    
    $array2 = array(
        'CA',
        'NY',
        'GA'
    );
    
    $array3 = array(
        'New York',
        'San Francisco',
        'Atlanta',
        'San Jose'
    );
    
    usort(
        $array1,
        function ($element1, $element2) use ($array2, $array3) {
            $state_sort_order = array_flip($array2);
    
            $element1_state_sort_order = $state_sort_order[$element1['state']];
            $element2_state_sort_order = $state_sort_order[$element2['state']];
    
            $state_sort_result = $element1_state_sort_order - $element2_state_sort_order;
    
            if ($state_sort_result === 0) {
                $city_sort_order = array_flip($array3);
                $element1_city_sort_order = $city_sort_order[$element1['city']];
                $element2_city_sort_order = $city_sort_order[$element2['city']];
    
                return $element1_city_sort_order - $element2_city_sort_order;
            } else {
                return $state_sort_result;
            }
        }
    );
    
    print_r($array1);
    

    Again, usort seemed to be the choice with the most flexibility. There might be a way to figure out how to use array_multisort, but I couldn’t tell based on the data you’ve given.

    I use a function closure, capturing $array2 and $array3 from the outer scope to do the sorting. It defines a comparison function for usort to apply to elements of $array1 to determine sort order. In this function, $element1 and $element2 each receive the value of elements from $array1 to process for comparison, e.g 'Smith,Atlanta,GA', or 'Mary,San Jose,CA'.

    For each element, I extract the city and state portions of the elements. If you flip $array2 and $array3, they become lookup tables for sort order when you index them by $city and $state, respectively.

    The rest is just returning the difference between city sort orders if the state sort orders are equal, else just return the state sort orders.

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

Sidebar

Related Questions

i have three columns for Date In, Date Out, Total Days. If in Date
I have two excel files. First excel file contains the Person Name and Total
In the multidimensional array below, I would like to merge arrays that have the
I have two PHP arrays. One contains a group name and another contains a
i have total 70,000 records in database. currently i displays all the records in
I have two total different implementations of AsyncTask , lets say AT1 and AT2
I have created total 50 test scripts. All these scripts use almost same objects
I have a total variable that I update with a refresh get request using
I have a problem with sql (maths). I have a total payable given to
I have one datatable. In that table I have columns named [Total Day], [Present

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.