Skip to content
Snippets Groups Projects
Commit ac3d64a9 authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

refactoring

parent b98a6c79
No related branches found
No related tags found
1 merge request!1add sortPrice
.vendor
\ No newline at end of file
<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/vendor/autoload.php';
$func = new Hp\Test\Functions();
use Hp\Test\Functions;
$func = new Functions();
?>
<!DOCTYPE html>
......@@ -17,11 +18,11 @@ $func = new Hp\Test\Functions();
<h1>Function 1</h1>
<?php
$array = [
['price'=>10, 'count'=>2],
['price'=>5, 'count'=>5],
['price'=>8, 'count'=>5],
['price'=>12, 'count'=>4],
['price'=>8, 'count'=>4],
['price'=>10, 'count'=>2],
['price'=>5, 'count'=>5],
['price'=>8, 'count'=>5],
['price'=>12, 'count'=>4],
['price'=>8, 'count'=>4],
];
print_r($func->sortPrice($array));
?>
......
Раз, два, три, четыре,
Пять, шесть, семь, восемь.
\ No newline at end of file
<?php
namespace Hp\Test;
declare(strict_types=1);
use DateTimeImmutable;
use DateTime;
use DateInterval;
use DatePeriod;
namespace Hp\Test;
class Functions {
class Functions
{
/**
* Выполняет сортировку массива по убыванию цены *
* @param array $array *
* @return array
*/
* Выполняет сортировку массива по убыванию цены
* @param array $array
* @return array
*/
function sortPrice(array $array): array {
array_multisort(array_column($array, 'price'), SORT_DESC, array_column($array, 'count'), SORT_ASC, $array);
public function sortPrice(array $array): array
{
$prices = array_column($array, 'price');
$counts = array_column($array, 'count');
array_multisort(
$prices,
SORT_DESC,
$counts,
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], ]
}
/**
* На выход должна вернуть отсортированный массив по ключу *price* DESC
* и во вторую очередь по *count* ASC:
* [['price'=>12, 'count'=>4], ['price'=>10, 'count'=>2], ['price'=>8, 'count'=>4],
* ['price'=>8, 'count'=>5], ['price'=>5, 'count'=>5],]
*/
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment