Commit f96f32f8 authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

refactoring

parent b046b15d
Loading
Loading
Loading
Loading

composer.lock

0 → 100644
+18 −0
Original line number Diff line number Diff line
{
    "_readme": [
        "This file locks the dependencies of your project to a known state",
        "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
        "This file is @generated automatically"
    ],
    "content-hash": "fb868aee4271b4f211fa6443b05e3b80",
    "packages": [],
    "packages-dev": [],
    "aliases": [],
    "minimum-stability": "stable",
    "stability-flags": [],
    "prefer-stable": false,
    "prefer-lowest": false,
    "platform": [],
    "platform-dev": [],
    "plugin-api-version": "2.6.0"
}
+7 −6
Original line number Diff line number Diff line
@@ -2,8 +2,9 @@

    require_once __DIR__ . '/vendor/autoload.php';

$func = new Hp\Test\Functions();
    use Hp\Test\Functions;

    $func = new Functions();
?>

<!DOCTYPE html>
@@ -21,7 +22,7 @@ $func = new Hp\Test\Functions();
            ['price'=>5, 'count'=>5],
            ['price'=>8, 'count'=>5],
            ['price'=>12, 'count'=>4],
            ['price'=>8, 'count'=>4]
            ['price'=>8, 'count'=>4],
        ];
        print_r($func->sortPrice($array));
    ?>

public/text.txt

deleted100644 → 0
+0 −2
Original line number Diff line number Diff line
Раз, два, три, четыре,
Пять, шесть, семь, восемь.
 No newline at end of file
+44 −30
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;
    }
}