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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:50:56+00:00 2026-05-27T16:50:56+00:00

I need to sort some data that is not coming from a database, but

  • 0

I need to sort some data that is not coming from a database, but is structured like a sql result set.

In MySQL, I would write a query as follows to sort the data by two columns:

SELECT product, qty FROM stock ORDER BY qty DESC, LENGTH(product) DESC 

However, in this case, I need to perform this sorting logic with php. Specifically, the rows are sorted first by descending qty, and then by the length of name descending.

Unsorted Input:

[
    ['name' => 'foo bar', 'qty' => 6],
    ['name' => 'foo bar bar foo', 'qty' => 10],
    ['name' => 'b', 'qty' => 5],
    ['name' => 'foo', 'qty' => 10],
    ['name' => 'bar', 'qty' => 6],
    ['name' => 'foo bar bar bar foo', 'qty' => 6],
]

After sorting, I need to restructure the data with the name values as keys and the qty values as values of a flat, associative array
The finished array would look something like this:

Desired Output:

[
    'foo bar bar foo' => 10,
    'foo' => 10,
    'foo bar bar bar foo' => 6,
    'foo bar' => 6,
    'bar' => 6,
    'b' => 5
]
  • 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-27T16:50:57+00:00Added an answer on May 27, 2026 at 4:50 pm

    Looking at the answers to this question: PHP array multiple sort – by value then by key?, it seems array_multisort is the way to go. (I’m not really sure how array_multisort works, I just kinda hacked this up, and it seems to work).

    Try this:

    $arr = array(
      'foo bar' => 6,
      'foo' => 10,
      'bar' => 6,
      'b' => 5,
      'foo bar bar bar foo' => 6,
      'foo bar bar foo' => 10
    );
    
    array_multisort(array_values($arr), SORT_DESC,
      array_map(create_function('$v', 'return strlen($v);'), array_keys($arr)),
      SORT_DESC, $arr);
    

    Demo: http://codepad.org/mAttNIV7

    UPDATE: Added array_map to make it sort by the length of the string, before it was just doing:

    $str1 > $str2 instead of strlen($str1) > strlen($str2).

    UPDATE 2: In PHP >= 5.3, you can replace create_function with a real anonymous function.

    array_map(function($v){return strlen($v);}, array_keys($arr))
    

    Demo 2: http://codepad.viper-7.com/6qrFwj

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

Sidebar

Related Questions

I need some sort of node-graph editor, that hopefully works on both Mac and
I have some sort of recursive function, but I need to parse a string,
I've written the following IComparer but I need some help. I'm trying to sort
I've got a lot of mysql data that I need to generate reports from.
I have a data set that I am reporting on. However, some of the
I have some data that looks a bit like this: require(zoo) X <- rbind(c(date='20111001',
I have a python function that scrapes some data from a few different websites
I need to setup some sort of loop that runs until an array has
I have a table with some data that looks like: id | type |
I need some sort of interactive chart control for my .NET-based web app. 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.