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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:12:32+00:00 2026-06-17T18:12:32+00:00

I want to sort the following array by multiple keys in the following order:

  • 0

I want to sort the following array by multiple keys in the following order: First by “type”, then “product”, and lastly by “name”. This is pretty easily done with usort, although my clients wants “product” sorted in a specific order: Stapler, Binder, Book.

$arr = array(
    array(
        'type' => 'School',
        'product' => 'Book',
        'name' => 'My book',
        'data' => '...'
    ),
    array(
        'type' => 'Job',
        'product' => 'Stapler',
        'name' => 'My stapler',
        'data' => '...'
    ),
    array(
        'type' => 'Personal',
        'product' => 'Binder',
        'name' => 'My binder',
        'data' => '...'
    ),
    array(
        'type' => 'School',
        'product' => 'Book',
        'name' => 'My book',
        'data' => '...'
    )
);

Does anyone know a clever way to accomplish this?

  • 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-17T18:12:33+00:00Added an answer on June 17, 2026 at 6:12 pm
    usort($arr, function ($a, $b) {
      // by type
      $r = strcmp($a['type'], $b['type']);
      if ($r !== 0) {
        return $r;
      }
    
      // by product
      // note: one might want to check if `$a/$b['product']` really is in `$order`
      $order = array('Stapler', 'Binder', 'Book');
      $r = array_search($a['product'], $order) - array_search($b['product'], $order);
      if ($r !== 0) {
        return $r;
      }
    
      // or similar, with a little help by @fab ;)
      /*
      $order = array('Stapler' => 0, 'Binder' => 1, 'Book' => 2);
      $r = $order[$a['product']] - $order[$b['product']];
      if ($r !== 0) {
        return $r;
      }
      */
    
      // name
      return strcmp($a['name'], $b['name']);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this following numpy matrix that I want to sort in ascending order
$array = array(2011-September_38,2011-June_4,2010-November_9,2011-November_29,2010-December_19); i want to sort this array strings as following, it should
Is there away i can sort the following array into the correct order? Array
I have the following array and I want to sort all the records with
I have the following array and I want to sort the subarrays by value
I have the following multi-dimensional array that I want to sort. I want to
I am having following array and I want to use search and sort.Search and
i have the following select box and want to sort it by display:none inlne
I want to be able to achieve the following using guice/gin: Get all sort
I want to sort a mutable array by date. My array contains several dict

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.