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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:49:16+00:00 2026-06-17T11:49:16+00:00

Possible Duplicate: How do I sort a multidimensional array in php Lets say i

  • 0

Possible Duplicate:
How do I sort a multidimensional array in php

Lets say i have following Multi-dimentional Array:

$fruits[] = array("name"=>"orange", "price"=>3, "code"=>"A45");
$fruits[] = array("name"=>"apple", "price"=>2, "code"=>"W71");
$fruits[] = array("name"=>"grape", "price"=>4, "code"=>"G11");

Then i wanted to sort this $fruits[] array by its price.
So it should be:

$fruits[] = array("name"=>"apple", "price"=>2, "code"=>"W71");
$fruits[] = array("name"=>"orange", "price"=>3, "code"=>"A45");
$fruits[] = array("name"=>"grape", "price"=>4, "code"=>"G11");

Then i surfed around web alot and finally i found following one from php manual (i adjusted the variable names with my array above):

// Obtain a list of columns
foreach ($fruits as $key => $value) {
    $name[$key]  = $value['name'];
    $price[$key] = $value['price'];
    $code[$key] = $value['code'];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($price, SORT_ASC, $fruits);

OK, got it! Then the $fruits[] array is sorted by price values properly.
Well, here are some of my concerns:

  • As far as I’ve learned before, we should not add/use any additional/customized codes to use a Sorting Machinism. (Instead, i believe we should use pure Built-in Methods.)
  • Here I doubt the performance because it is having pre-custom foreach loop inside, which may dramatically bring down the speed while we are having real huge arrays. (like i have now)

So now what i want to ask is:

  • Does PHP has any pure Method to achieve this thing?
  • [or] Can’t we just rectify this Multi-dimentional Array Sorting Issue by only using/picking from some of PHP pure built-in, array-sorting Methods (e.g: sort(), usort(), asort(), array_multisort(), etc..) ?
  • 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-17T11:49:17+00:00Added an answer on June 17, 2026 at 11:49 am
    usort ($fruits, function($a, $b) { return $a['price'] > $b['price'] ? 1 : ($a['price'] == $b['price'] ? 0 : -1); });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: php sort array by sub-value I have a multidimensional array like the
Possible Duplicate: How do I sort a multidimensional array in php I have value
Possible Duplicate: PHP Sort a multidimensional array by element containing date I have some
Possible Duplicate: How do I sort a multidimensional array in php I have multidimensional
Possible Duplicate: How do I sort a multidimensional array in php I have an
Possible Duplicate: How do I sort a multidimensional array in php i need to
Possible Duplicate: How do I sort a multidimensional array in php Sorting an associative
Possible Duplicate: How do I sort a multidimensional array in php Array ( [0]
Possible Duplicate: Sorting an associative array in PHP How to sort a multidimensional array
Possible Duplicate: Sorting multidimensional array in PHP How can I sort by key in

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.