Skip to content

Instantly share code, notes, and snippets.

@palexandrite
Last active April 11, 2023 08:13
Show Gist options
  • Save palexandrite/2971c2cb55c59acd7010731766785a3f to your computer and use it in GitHub Desktop.
Save palexandrite/2971c2cb55c59acd7010731766785a3f to your computer and use it in GitHub Desktop.
How to pluralize words by PHP and find an array with max set of values when we use only 0 and 1.
<?php
$array = [
[0, 0, 0, 1],
[0, 0, 1, 1],
[0, 1, 1, 1],
[1, 1, 1, 1]
];
function find($arr)
{
$new = [];
foreach ($arr as $item) {
$new[] = array_sum($item);
}
return max($new);
}
echo 'Это максимальная строка: '. find($array)."<br>";
class Helper
{
/**
* Выбирает слово с правильными окончанием после числительного.
*
* @param int $number число
* @param array $words варианты склонений ['яблоко', 'яблока', 'яблок']
* @return string
*/
public static function plural(int $number, array $words): string
{
return $words[($number % 100 > 4 && $number % 100 < 20) ? 2 : [2, 0, 1, 1, 1, 2][min($number % 10, 5)]];
}
}
$count = 10;
echo "Загружено $count " . Helper::plural($count, ['запись', 'записи', 'записей']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment