function getRandomWinner($ppl)
{
$choice = rand(0, array_sum(array_values($ppl)) - 1);
$sum = 0;
foreach($ppl as $name => $rank) {
$sum += $rank;
if ($choice < $sum) return $name;
}
}
$winnerName = getRandomWinner([
'Иван' => 60,
'Максим' => 20,
'Вова' => 20,
]);
document.querySelector('.case-print')
$result = [];
$limit = count($arr2);
foreach ($arr1 as $index => $item) {
$result[] = $item . $arr2[$index % $limit];
}
$result = array_map(function($item, $i) use ($arr2) {return $item . $arr2[$i % count($arr2)];}, $arr1, array_keys($arr1));
const buttons = {};
'all,love,chef,girl,boy,grandma,grandpa'
.split(',').forEach((name) => buttons[name] = menuWrapper.querySelector('.' + name));
Использовать потом, например, вместо girlButton
— buttons.girl
select *, price * (73 - (72 * valuta)) as price_in_rub
from ...
where ...
n % 10
const lastDigit = n => {
// в n совсем не то
if (isNaN(n) || !isFinite(n)) return NaN;
// в n целое
if (n % 1 === 0) return n % 10;
// для дробных проще со строкой работать
const s = String(Math.abs(n));
// неточные значения
if (s.length > 16 || s.includes('e')) return NaN;
return +s.slice(-1);
}
const lastDigit = 765 % 10; // -> 5
[...new Set(data.variations.map(n => n.color.name))]
Object.values(Object.fromEntries(data.variations.map(n => [ n.color.name, n.color ])))
// или
Object.values(data.variations.reduce((acc, { color: n }) => (acc[n.name] ??= n, acc), {}))
// или
data.variations.map(n => n.color).filter(function(n) {
return !(this[n.name] = this.hasOwnProperty(n.name));
}, {})
const unique = (data, key = n => n) =>
Array.prototype.filter.call(data, function(n) {
const k = key(n);
return !this.has(k) && this.add(k);
}, new Set);
// получаем массив уникальных имён цветов
const uniqueStrColors = unique(data.variations.map(n => n.color.name));
// получаем массив объектов цветов, свойства name которых уникальны
const uniqueObjColors = unique(data.variations.map(n => n.color), n => n.name);
class DbConnection
{
private string $userName;
private string $password;
private string $dbname;
public function __construct(string $userName, string $password, string $dbName)
{
$this->userName = $userName;
$this->password = $password;
$this->dbname = $dbName;
}
}
class AuthController
{
private DbConnection $dbConnection;
public function __construct(DbConnection $connection)
{
$this->dbConnection = $connection;
}
}
$dbSettings = include('dbconfig.php');
$connection = new DbConnection($dbSettings['login'], $dbSettings['password'], $dbSettings['dbName'] );
$controller = new AuthController($connection);
return [
'login'=>'myLogin',
'password'=>'myPassword',
'dbName'=>'myDbName'
];
docker run --rm -v ${PWD}/:/var/task -u 0 node:15-alpine sh --help
BusyBox v1.31.1 () multi-call binary.
Usage: sh [-/+OPTIONS] [-/+o OPT]... [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS] / -s [ARGS]]
Unix shell interpreter
docker run --rm -v ${PWD}/:/var/task -u 0 node:15-alpine sh -c "cd /var/task && npm install && npm ci"
docker run --rm -v ${PWD}/:/var/task -u 0 --workdir="/var/task" node:15-alpine sh -c "npm install && npm ci
docker run --rm -v ${PWD}/:/var/task -u 0 --workdir="/var/task" node:15-alpine npm install && npm ci
<?php
$query = "select * from equipment";
$stmt = $pdo->prepare($query);
$stmt->execute();
$equipments = $stmt->fetchAll(PDO::FETCH_ASSOC);
$result = array_reduce(
$equipments,
function($res, $el) {
if(!is_array($res[$el["equipment_id"]])) {
$res[$el["equipment_id"]] = [
"equipment_id" => 141491,
"speed" => []
];
}
array_push(
$res[$el["equipment_id"]]["speed"],
[
"datetime" => $el["datetime"],
"value" => $el["speed"],
"distance" => $el["distance"],
]
);
return $res;
},
[]
);
var_export(array_values($result));
<?php
$query = "select
equipment_id,
json_arrayagg(
json_object(
'datetime', `datetime`,
'value', `speed`,
'distance', `distance`
)
) speed
from equipment
group by equipment_id;";
$stmt = $pdo->prepare($query);
$stmt->execute();
$equipments = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_export($equipments);