How to sort an array of associative arrays by value of a given key in PHP

0 votes

Given this array:

$inventory = array(

   array("type"=>"fruit", "price"=>3.50),
   array("type"=>"milk", "price"=>2.90),
   array("type"=>"pork", "price"=>5.43),

);

I would like to sort $inventory's elements by price to get:

$inventory = array(

   array("type"=>"pork", "price"=>5.43),
   array("type"=>"fruit", "price"=>3.50),
   array("type"=>"milk", "price"=>2.90),

);

How can I do this?

Jul 24 in PHP by Kithuzzz
• 4,480 points
20 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
1 answer

How to delete element by value in array (not key) using php?

Hello @kartik, Using array_search() and unset, try the following: if (($key = ...READ MORE

answered Sep 16, 2020 in PHP by Niroj
• 82,740 points
221 views
0 votes
1 answer

How to check an IP address is within a range of two IPs in PHP?

Hello @kartik, With ip2long() it's easy to convert your addresses ...READ MORE

answered Nov 3, 2020 in PHP by Niroj
• 82,740 points
1,918 views
0 votes
1 answer

How to access array returned by a function in php?

Hello @kartik, Since PHP 5.4 it's possible to ...READ MORE

answered Nov 10, 2020 in PHP by anonymous
• 82,740 points
359 views
0 votes
0 answers

How to convert an array to a string in PHP?

What can I do to get the ...READ MORE

May 30 in PHP by Kichu
• 19,040 points
24 views
0 votes
0 answers

How to convert a string to an array in php

I want to convert a string into ...READ MORE

Jun 1 in PHP by Kichu
• 19,040 points
26 views
0 votes
0 answers

How can I check if an array contains a specific value in php?

I have a PHP variable of the ...READ MORE

Jun 4 in PHP by Kichu
• 19,040 points
10 views
0 votes
0 answers

How to Sort a Multi-dimensional Array by Value

How can I sort this array by ...READ MORE

Jul 29 in PHP by Kithuzzz
• 4,480 points
14 views
0 votes
1 answer

How to check if array is multidimensional or not?

Since the 'second dimension' could be just ...READ MORE

answered Nov 5, 2018 in Others by DataKing99
• 8,240 points
3,979 views
0 votes
0 answers

How can I sort arrays and data in PHP?

How do I sort an array in ...READ MORE

May 29 in PHP by Kichu
• 19,040 points
12 views
0 votes
0 answers

PHP multidimensional array search by value

I want to search for the uid ...READ MORE

May 30 in PHP by Kichu
• 19,040 points
17 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP