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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:18:06+00:00 2026-05-28T08:18:06+00:00

Well I am trying to sort some data in PHP. Here is an example

  • 0

Well I am trying to sort some data in PHP. Here is an example of the array(the first column representing ids I want to still be associated with their respective values in each row):

0 1 2.0
1 15 20.0
2 15 5.5
3 15 55.1
4 2 22.3
5 20 70.8
6 2 8.2

First I would like to arrange the rows in an order where the values in the second column are in decending order:

5 20 70.8
1 15 20.0
2 15 5.5
3 15 55.1
4 2 22.3
6 2 8.2
0 1 2.0

Then, keeping those with the second column still in its arrangement, by each set of rows with the same value in the second column, arrange the third values in ascending order:

5 20 70.8
2 15 5.5
1 15 20.0
3 15 55.1
6 2 8.2
4 2 22.3
0 1 2.0

I have tried some things with the array sorting functions for PHP, however I can’t figure out how to do both those operations while still maintaining row association.

  • 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-28T08:18:07+00:00Added an answer on May 28, 2026 at 8:18 am

    One way is to use the usort function to create a custom sorting routine. There is quite a large number of ways to sort arrays in PHP depending on how you want the data sorted, multi-dimensionality, key sorts, etc. This snippet sorts based on the input and output requirements stated above.

    $vals = array(
        array('id' => 0, 'val1' => 1, 'val2' => 2.0),
        array('id' => 1, 'val1' => 15, 'val2' => 20.0),
        array('id' => 2, 'val1' => 15, 'val2' => 5.5),
        array('id' => 3, 'val1' => 15, 'val2' => 55.1),
        array('id' => 4, 'val1' => 2, 'val2' => 22.3),
        array('id' => 5, 'val1' => 20, 'val2' => 70.8),
        array('id' => 6, 'val1' => 2, 'val2' => 8.2)
        );
    
    usort($vals, 'sortfn');
    
    function sortfn($a, $b)
    {
        if($a['val1'] == $b['val1'])
            return ($a['val2'] > $b['val2']);
        else
            return ($a['val1'] < $b['val1']);
    }
    
    var_dump($vals);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well I am working with wordpress and I'm trying to sort some posts by
When trying to link some well established tools to my company's active directory, I
Well, here's a nice obscure one. I'm trying to compile the latest open transport
I'm trying to display frequencies within barplot ... well, I want them somewhere in
I'm trying to find some sort of a good, fuzzy string matching algorithm. Direct
I have an asp.net page that has some sort of data load problem. I'm
I am trying to write a JTextPane which supports some sort of coloring: as
Here's a fun little script I'm trying to make. Flash words from an array
I'm trying to sort some DIVS in different ways and I'm quite lost. I've
trying to have some sort of gradiented border by having a parent element have

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.