Loading .gitignore 0 → 100644 +1 −0 Original line number Diff line number Diff line /vendor No newline at end of file index.php +8 −6 Original line number Diff line number Diff line Loading @@ -2,8 +2,9 @@ require_once __DIR__ . '/vendor/autoload.php'; $func = new Hp\Test\Functions(); use Hp\Test\Functions; $func = new Functions(); ?> <!DOCTYPE html> Loading Loading @@ -37,6 +38,7 @@ $func = new Hp\Test\Functions(); print_r($func->search($array, 54)); ?> <h1>Function 3</h1> <?php $arr = [ Loading src/Functions.php +54 −39 Original line number Diff line number Diff line <?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 * * Выполняет сортировку массива по убыванию цены * @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],] */ /* Найдет элемент с указаным id * * @param array $array - массив, содержащий элементы со структурой * [ * 'id' => 30, * 'name' => 'Jhon', * 'age' => 23, * ] * @param $id - ид искомого элемента * * @return array - найденный элемент /** * Найдет элемент с указаным id * @param array $array - массив, содержащий элементы со структурой * [ * 'id' => 30, * 'name' => 'Jhon', * 'age' => 23, * ] * @param $id - ид искомого элемента * @return ?array - найденный элемент */ function search($array, $id): ?array { $rowId = array_search($id, array_column($array, 'id')); public function search(array $array, int $id): ?array { $rowId = array_search($id, array_column($array, 'id'), true); if ($rowId) { return $array[$rowId]; } return null; } /** * Удалить дубликаты, оставив только уникальные значения Loading @@ -45,12 +59,13 @@ class Functions { * @return array */ function uniqElements(array $array): array { public function uniqElements(array $array): array { return array_unique($array, SORT_REGULAR); } //Выходной массив: /** Array ( /** * Выходной массив: * Array ( * [0] => Array([0] => laravel, [1] => php) * [1] => Array([0] => codeigniter, [1] => php) * [3] => Array([0] => c++, [1] => java)) Loading Loading
.gitignore 0 → 100644 +1 −0 Original line number Diff line number Diff line /vendor No newline at end of file
index.php +8 −6 Original line number Diff line number Diff line Loading @@ -2,8 +2,9 @@ require_once __DIR__ . '/vendor/autoload.php'; $func = new Hp\Test\Functions(); use Hp\Test\Functions; $func = new Functions(); ?> <!DOCTYPE html> Loading Loading @@ -37,6 +38,7 @@ $func = new Hp\Test\Functions(); print_r($func->search($array, 54)); ?> <h1>Function 3</h1> <?php $arr = [ Loading
src/Functions.php +54 −39 Original line number Diff line number Diff line <?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 * * Выполняет сортировку массива по убыванию цены * @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],] */ /* Найдет элемент с указаным id * * @param array $array - массив, содержащий элементы со структурой * [ * 'id' => 30, * 'name' => 'Jhon', * 'age' => 23, * ] * @param $id - ид искомого элемента * * @return array - найденный элемент /** * Найдет элемент с указаным id * @param array $array - массив, содержащий элементы со структурой * [ * 'id' => 30, * 'name' => 'Jhon', * 'age' => 23, * ] * @param $id - ид искомого элемента * @return ?array - найденный элемент */ function search($array, $id): ?array { $rowId = array_search($id, array_column($array, 'id')); public function search(array $array, int $id): ?array { $rowId = array_search($id, array_column($array, 'id'), true); if ($rowId) { return $array[$rowId]; } return null; } /** * Удалить дубликаты, оставив только уникальные значения Loading @@ -45,12 +59,13 @@ class Functions { * @return array */ function uniqElements(array $array): array { public function uniqElements(array $array): array { return array_unique($array, SORT_REGULAR); } //Выходной массив: /** Array ( /** * Выходной массив: * Array ( * [0] => Array([0] => laravel, [1] => php) * [1] => Array([0] => codeigniter, [1] => php) * [3] => Array([0] => c++, [1] => java)) Loading