Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
/**
* Выполняет сортировку массива по убыванию цены *
* @param array $array *
* @return array
*/
function sortPrice(array $array): array {
$price = [];
$count = [];
foreach ($array as $key => $row) {
$price[$key] = $row['price'];
$count[$key] = $row['count'];
}
array_multisort($price, SORT_DESC, $count, SORT_ASC, $array);
return $array;
}
//На выход должна вернуть отсортированный массив по ключу *price* DESC и во вторую очередь по *count* ASC:
//[ ['price'=>12, 'count'=>4], ['price'=>10, 'count'=>2], ['price'=>8, 'count'=>4], ['price'=>8, 'count'=>5], ['price'=>5, 'count'=>5], ]
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$array = [
['price'=>10, 'count'=>2],
['price'=>5, 'count'=>5],
['price'=>8, 'count'=>5],
['price'=>12, 'count'=>4],
['price'=>8, 'count'=>4],
];
print_r(sortPrice($array));
?>
</body>
</html>