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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:59:30+00:00 2026-06-10T04:59:30+00:00

I have an array that I’m simply trying to order by a score key

  • 0

I have an array that I’m simply trying to order by a score key that contains the value that I’m trying to sort by. The desired results would change the order of the array below to display the index in the following order: [0], [2], [1].

I’m looping through the results which are pulled from a database. I’m creating a key named score and then pushing the $row2 array in to a separate array because if I use mysql_data_seek it gets rid of the score key.

Also it’s creating an unwanted key score at the bottom.

How can I best clean this up and make sure the results are ordered high to low as desired?

$my_array = array();

while ($row2 = mysql_fetch_assoc($result2))
{
 //score determined here
 //$row2['score'] = ...more math, variable is numeric, not a string
 array_push($array_page,$row2);
}

Current undesired results…

Array
(
    [0] => Array
        (
            [score] => 7
        )
    [1] => Array
        (
            [score] => 2
        )
    [2] => Array
        (
            [score] => 4
        )
    [score] => 
)

Desired results…

Array
(
    [0] => Array
        (
            [score] => 7
        )
    [2] => Array
        (
            [score] => 4
        )
    [1] => Array
        (
            [score] => 2
        )
)
  • 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-10T04:59:32+00:00Added an answer on June 10, 2026 at 4:59 am
    function scoreSort($a, $b){
      if($a['score'] == $b['score']) return 0;
      return $a['score'] > $b['score'] ? -1 : 1;
    }
    usort(&$myArray, 'scoreSort');
    

    on php>5.3 you can use inline-functoins:

    usort(&$myArray, function($a,$b){ /* ... */ });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that contains several arrays and I would like to order
I have an array that I would like to sort, that should be easy,
I have an array that contains the location of a value in a very
I have array that I can display by code: foreach($form->data as $key => $value){
I have an array that contains @sign in the object. I can't access that
I have an Array that contains some elements multiple times. Now I want to
I have an Array that returns a requested series of dates that I would
I have an array that contains string with the following format: 1-a1,1-a2,1-a3,1-a4,2-b1,2-b2,2-b3,2-b4,3-c1,3-c2,3-c3,4-d1,4-d2 where a
I have an array that contains a custom class of mine called 'Game'. Each
I have a array that contains all the months in a year like so:

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.