$arr = [
[
'COLOR' => 'Red',
'WEIGHT' => 20
],
[
'COLOR' => 'Red',
'WEIGHT' => 25
],
[
'COLOR' => 'Red',
'WEIGHT' => 30
],
[
'COLOR' => 'Green',
'WEIGHT' => 20
],
[
'COLOR' => 'Green',
'WEIGHT' => 25
],
[
'COLOR' => 'Green',
'WEIGHT' => 30
]
];
$out = [];
foreach ($arr as $a) {
$out[$a['COLOR']][] = $a['WEIGHT'];
}
foreach (array_map(function($a){ return join(', ', $a); }, $out) as $k => $item) {
echo "$k $item<br>";
}
~@[\h\pL]+\(([^)]+)\)\h\{([^}]+)\}\K~
$arr = [
'863|20',
'863|50',
'822|100',
'822|100'
];
$out = [];
foreach ($arr as $item) {
[$k, $v] = explode('|', $item);
$out[$k][] = $v;
}
var_dump($out);
array (size=2)
863 =>
array (size=2)
0 => string '20' (length=2)
1 => string '50' (length=2)
822 =>
array (size=2)
0 => string '100' (length=3)
1 => string '100' (length=3)
$arr = [
'863|20',
'863|50',
'822|100',
'822|100'
];
$out = [];
foreach ($arr as $item) {
[$k, $v] = explode('|', $item);
$out[$k] = ($out[$k] ?? 0) + $v;
}
var_dump($out);
/*
array (size=2)
863 => int 70
822 => int 200
*/
function formatNumber(int $int): string
{
$k = (int) log($int, 1000);
$float = round($int / pow(1000, $k), 1);
$letters = ['К', 'М', 'Млрд'];
return $float . ($letters[--$k] ?? '');
}
echo formatNumber(7); // 7
echo formatNumber(78); // 78
echo formatNumber(789); // 789
echo formatNumber(7899); // 7.9К
echo formatNumber(78991); // 79К
echo formatNumber(789919); // 789.9К
echo formatNumber(7899191); // 7.9М
echo formatNumber(78991911); // 79М
echo formatNumber(789919119); // 789.9М
echo formatNumber(7899191191); // 7.9Млрд
echo formatNumber(78991911910); // 79Млрд
$string = '40|https://site1.com/files/02bea218b601ef2cbc74e08dc8d78_1000_1000.png;77|https://site1.com/files/29037cbdc27707fe2d6b2cf4d3924_1000_1000.png;78|https://site1.com/files/923b6f214f8bffcf20fbbebe5365b_1000_1000.png;85|https://site1.com/files/f8b6dbe261e89877e23e6a6f00003_1000_1000.png';
$result = rtrim(preg_replace('~\|[^;]+(?:;|$)~', ',', $string), ',');
echo $result; // 40,77,78,85
$json = '{"v": 50, "vd": 5},
{"v": 51, "vd": 1},
{"v": 54, "vd": 3},
{"v": 57, "vd": 3},
{"v": 60, "vd": 3},
{"v": 61, "vd": 1},
{"v": 62, "vd": 1},
{"v": 63, "vd": 1},
{"v": 63, "vd": 0}';
$array = array_map(function($a){ return $a['v']; }, json_decode("[$json]", true));
var_dump($array);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$email = $_POST['email'] ?? '';
$message = $_POST['email'] ?? '';
$error = '';
if (trim($email) == ''){
$error = 'Введите ваш email';
} elseif (trim($message) == '') {
$error = 'Введите сообщение';
} elseif (strlen($message) < 10) {
$error = 'Сообщение должно быть более 10 символов';
}
if ($error) {
echo $error;
} else {
$subject = "=?utf-8?B?" . base64_encode("Тестовое сообщение") . "?=";
$headers = "From: $email\r\nReplay-to: $email \r\nContent-type: text/html;charset=utf-8\r\n";
mail('dferqqw@mail.ru', $subject, $message, $headers);
}
}
$arr = [
[
"url" => "tut url",
"format" => "mp4, video, 720p"
],
[
"url" => "tut url",
"format" => "mp4, video, 480p"
],
[
"url" => "tut url",
"format" => "mp4, video, 360p"
]
];
$arr = array_filter($arr, function($a){
return $a['format'] == "mp4, video, 720p" || $a['format'] == "mp4, video, 480p";
});
var_dump($arr);
? function formatNumber(int $int): String
{
$k = (int) log($int, 1000);
$float = round($int / pow(1000, $k), 1);
$letters = ['К', 'М', 'Млрд'];
return $float . ($letters[--$k] ?? '');
}
// Примеры использования:
echo formatNumber(7); // 7
echo formatNumber(78); // 78
echo formatNumber(789); // 789
echo formatNumber(7899); // 7.9К
echo formatNumber(78991); // 79К
echo formatNumber(789919); // 789.9К
echo formatNumber(7899191); // 7.9М
echo formatNumber(78991911); // 79М
echo formatNumber(789919119); // 789.9М
echo formatNumber(7899191191); // 7.9Млрд
echo formatNumber(78991911910); // 79Млрд
Есть ли спобоб без цикла проверить вхождение каждого элемента массива в строку?Такого способа нет, т.к. любая функция для обработки массива "под капотом" реализует цикл.
<?php
session_start();
require('connect.php');
if (isset($_POST['username']) and isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username='$username' and password='$password'";
$query2 = "SELECT * FROM users WHERE role = 'admin'";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
$result2 = mysqli_query($connection, $query2) or die(mysqli_error($connection));
$count = mysqli_num_rows($result);
$role = [];
while ($row = mysqli_fetch_array($result2)) {
$role[] = $row['username'];
}
if (in_array($username, $role)) {
header('Location:admin.php');
$_SESSION['t'] = "Вы вошли под админом ";
} else {
header('Location: index.html');
echo "Вы вошли под пользователем ";
}
if ($count == 1) {
$_SESSION['username'] = $username;
} else {
$fsmsg = "Ошибка";
}
}
if (isset($_SESSION['username'])) {
$username = $_SESSION['username'];
echo "Hello," . $username . "";
echo "<a href='logout.php' class='btn btn-lg btn-primary'>Выйти</a>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
<title>Авторизация</title>
</head>
<body>
<div class="container">
<form class="form-signin" method="POST">
<h2>Авторизация</h2>
<input type="text" name="username" class="form-control" placeholder="Username" required>
<input type="password" name="password" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Авторизоваться</button>
<a href="index.php" class="btn btn-lg btn-primary btn-block">Зарегистрироваться</a>
</form>
</div>
</body>
</html>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$dirname = trim($_POST['dir'] ?? '');
if ($dirname && mkdir($dirname)) {
echo "Директория создана";
} else {
echo "Не удалось создать директорию";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="dir">
<input type="submit">
</form>
</body>
</html>