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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:07:58+00:00 2026-06-10T21:07:58+00:00

I currently have an associative array of values that I need sorted based on

  • 0

I currently have an associative array of values that I need sorted based on values in another array.

Example:

If I have an array of values,
AssocArray = ‘122’=>’12’, ‘123’=>’32’, ‘232’=>’54’, ‘343’=>’12’

I need to check it against another array of values,
orderArray = ‘232’, ‘123’

if it exists then push the value to the top of the AssocArray. so the final array looks like this.

AssocArray = ‘232’=>’54’, ‘123’=>’32’, ‘122’=>’12’, ‘343’=>’12’

Sorry I do not have any working code, I am still learning PHP.

Any help would be really grateful 🙂 Thank you.

  • 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-10T21:08:00+00:00Added an answer on June 10, 2026 at 9:08 pm

    Loop through your $orderArray, and check if an element with that key is present in $AssocArray. If it is, add that element to the $result array, and remove it from the original $AssocArray. Then, you just have to merge the remaining elements in $AssocArray with whatever was pushed to the top of the $results array:

    $AssocArray = array( '122'=>'12', '123'=>'32', '232'=>'54', '343'=>'12');
    $orderArray = array( '232', '123');
    
    rsort( $orderArray, SORT_STRING); // Make sure the order array is sorted from highest to lowest
    
    $result = array();
    foreach( $orderArray as $key) {
        if( isset( $AssocArray[ $key ])) {
            $result[$key] = $AssocArray[ $key ];
            unset( $AssocArray[ $key ]);
        }
    }
    
    foreach( $AssocArray as $k => $v) {
        $result[$k] = $v;
    }
    
    print_r( $result); // Print the resulting array
    

    You can see that this prints:

    Array
    (
        [232] => 54
        [123] => 32
        [122] => 12
        [343] => 12
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have an array, created from a database, an example of which looks
I have a function that generates a dropdown based on a query or array
Currently have a drop down menu that is activated on a hover (from display:none
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty
I currently have one project that currently contains multiple packages. These packages make up
I currently have a deployed app (fortworth.herokuapp.com) that I am attempting to sort movies
I have a foreach loop in php to iterate an associative array. Within the
I have a JavaScript associative array (or some may prefer to call it an
What is the best way to have an associative array with arbitrary value types
I have a complicated background image with a lot of small regions that need

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.