Skip to content
Snippets Groups Projects
index.php 1.21 KiB
Newer Older
Адлан Шамавов's avatar
Адлан Шамавов committed
<?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>