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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:12:49+00:00 2026-05-30T08:12:49+00:00

I am trying to calculate percentiles for users in a database. In order to

  • 0

I am trying to calculate percentiles for users in a database. In order to do this I have a $data array that I need to sort.

I have a query object that contains User_ID, Total_User_Orders, and Total_Orders. Here’s how the code looks so far:

// Loop through the users
foreach($query->result() as $row)
{
    (string)$user_id = (string)$row->user_id;

    $data[$user_id] = number_format((($row->total_user_orders/$row->total_orders)*100), 5);
}

// Sort the $data array
array_multisort($data);

print_r($data);

What (I believe) that should do is typecast $row->user_id (an int) as a string. Then, the $data[$user_id] index should be set as a string – right…?

When I sort the array using array_multisort it sorts it as though the index was an Integer, rather than a String. This means it loses the index.

The PHP manual for array_multisort() states, “Associative (string) keys will be maintained, but numeric keys will be re-indexed.”. I have also tried using array_multisort($data, SORT_STRING), but the same output occurs. However – it does work when I do $data['#'.$user_id], but this doesn’t quite feel like the right solution to me!

Can anyone help? Thanks in advance!

  • 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-30T08:12:50+00:00Added an answer on May 30, 2026 at 8:12 am

    I think you’re overcomplicating things. With no testing, I’d think indexing the $data-array like this would work:

    $data[(string)$row->user_id] = ...
    

    or

    $data[''.$user_id] = ...
    

    EDIT:
    Otherwise you could build your array multi-dimensional and sort by one of the indices, like this:

    foreach($query->result() as $row) {
      $data[] = array(
        'user_id' => $row->user_id,
        'percent' => number_format((($row->total_user_orders/$row->total_orders)*100), 5);
      );
    }
    

    Or you could index by the percentage and sort by the keys (using ksort()):

    foreach($query->result() as $row) {
      $data[number_format((($row->total_user_orders/$row->total_orders)*100), 5)] = $row->user_id];
    }
    

    The last solution could be dangerous if several users have the same percentage.

    Personally I would probably go with the asort() solution mentioned above.

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

Sidebar

Related Questions

I am trying to calculate number of users, cumulatively for the dellstore2 database. Looking
I'm trying to calculate the time it takes to receive all data from a
I'm trying to calculate how many minutes have elapsed between a saved DateTime and
I'm trying to calculate the Mean Difference average of a set of data. I
I'm trying to calculate (dynamically) the % of the year that has elapsed, or
Trying to calculate sum of checked tr's. var totalqt=0; totalqt=totalqt + $(this).closest(tr).find(#qt).text(); It gets
I am trying to calculate this if/else statement through the JOption Message window. Prompt
I'm trying to calculate the 1st and 3rd Quartile values for an array of
I am trying to calculate the fiscal year from this following code. But I
I'm trying to calculate the load on a server I have to build. I

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.