I
I
Iskander Mamedov2016-02-09 10:25:29
PHP
Iskander Mamedov, 2016-02-09 10:25:29

How to sort multidimensional associative array in PHP?

I have an array like

$flats[0] = array("rooms"=>"3", "price"=>"1500", "url"=>"http://vk.com");
$flats[1] = array("rooms"=>"1", "price"=>"300", "url"=>"http://vk.com");
$flats[2] = array("rooms"=>"2", "price"=>"300", "url"=>"http://vk.com");
$flats[3] = array("rooms"=>"3", "price"=>"700", "url"=>"http://vk.com");

How can I sort it by "price" value? I read that it is possible through array_multisort, but did not figure out how. I will be very grateful for your answer :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2016-02-09
@twist4

php.net/manual/en/function.uasort.php
ideone.com/kNt2MR

K
krypt3r, 2016-02-09
@krypt3r

Using array_multisort() something like this

$prices = array();
foreach ($flats as $i => $row) {
    $prices[$i] = (int) $row['price'];
}
array_multisort($prices, SORT_ASC, SORT_NUMERIC, $flats);
print_r($flats);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question