$res = array_reduce(
$data,
function($res, $el) {
// Проверяет, есть ли пара имени бренда в массиве результатов
if (!isset($res[$el['name'].$el['brand']])) {
$res[$el['name'].$el['brand']] = $el;
} elseif (
// если пара имени бренда в массиве результатов проверяет цену
$res[$el['name'].$el['brand']]['price'] > $el['price']
) {
$res[$el['name'].$el['brand']]['price'] = $el['price'];
}
return $res;
},
[]
);
usort(
$arr,
fn($a, $b) => array_search($a['id'], $ids) <=> array_search($b['id'], $ids)
);
SELECT posts.id_post, posts.text_post, posts.photo, posts.created_at,
users.id_user, users.login, users.name, users.surname, users.avatar
COALESCE(COUNT(`likes`.`id_post`), 0) AS `likes`
FROM `posts`
INNER JOIN `users_connections` ON posts.id_user = users_connections.id_followed
INNER JOIN `users` ON users.id_user = posts.id_user
LEFT JOIN `likes` ON `likes`.`id_post` = `posts`.`id_post`
WHERE users_connections.id_follower = :id_follower
AND `created_at` BETWEEN '$prewweek' AND '$nowdate' ORDER BY `created_at`
GROUP BY posts.id_post, posts.text_post, posts.photo, posts.created_at,
users.id_user, users.login, users.name, users.surname, users.avatar
echo json_encode($result, JSON_NUMERIC_CHECK);
if (preg_match('/^\+7 \(\d{3}\) \d{3}-\d{2}-\d{2}$/', $login_phone)) {
echo 'ok';
} else {
echo 'wrong number';
}
preg_match_all('/<paramname>(.+)<\/paramname>/', $text, $matches); // <![CDATA[Модель]]>
preg_match_all('/<paramname><!\[CDATA\[(.+)\]\]><\/paramname>/', $text, $matches); //Модель
<?php
function get_categoies($pdo)
{
$stmt = $pdo->prepare("SELECT * FROM `categories`");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function get_prices_by_category($pdo, $category_id)
{
$stmt = $pdo->prepare("SELECT * FROM `prices` WHERE category_id = ?");
$stmt->execute([$category_id]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
$categories = get_categoies($pdo);
foreach ($categories as $category) {
echo '<h3>'.$category['title'].'</h3>
<div class="cblock">';
$prices = get_prices_by_category($pdo, $category['id']);
foreach ($prices as $price) {
echo '<div class="pricelist-row">
<div class="service">' . $price["title"] . '</div>
<div class="price">от ' . $price["price"] . ' руб/' . $price["type"] . '</div>
</div>';
}
echo '</div>';
}
<?php
//Есть массив
$arr=['a','d','x','d','d','d','a','z','z','f','f','f','f','b','c','v'];
echo '<div>' . PHP_EOL;
foreach ($arr as $i=>$el) {
if ($i > 0 && $el <> $arr[$i - 1] ) echo '</dv><div>' . PHP_EOL;
echo $el . PHP_EOL;
}
echo '</div>';
$res = array_filter(
$arr,
fn($el)=>!in_array($el["uniq_id"], $filter)
);
create table class (
id int primary key auto_increment,
name varchar(64) unique key
);
create table subclass (
id int primary key auto_increment,
class_id int references class(id),
name varchar(64) unique key
);
create table items (
id int primary key auto_increment,
subclass_id int references subclass(id),
name varchar(64) unique key
);
insert into class (name) values ('Fruits'), ('Vegetables');
insert into subclass (class_id, name) values (1, 'Red'), (1, 'Orange'), (2, 'Green'), (2, 'Yellow');
insert into items (subclass_id, name) values
(1, 'Apple'), (2, 'Pineapple'), (3, 'Cucumber'), (3, 'Carrot');
<?php
$query = 'select class.name class, subclass.name subclass, items.name item
from items
join subclass on items.subclass_id = subclass.id
join class on subclass.class_id = class.id;';
// get DB version using PDO
$stmt = $pdo->prepare($query);
$stmt->execute();
$res = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
if (!isset($res[$row["class"]])) {
$res[$row["class"]] = [];
}
if (!isset($res[$row["class"]][$row["subclass"]])) {
$res[$row["class"]][$row["subclass"]] = [];
}
$res[$row["class"]][$row["subclass"]][] = $row["item"];
}
print_r(json_encode($res, JSON_PRETTY_PRINT));
<?php
function isSequence3($num) {
if ($num < 123) return false;
$digits = str_split($num);
$seq = 1;
for($i=1; $i<count($digits); $i++) {
if ($digits[$i] == ($digits[$i - 1] + 1)) $seq++;
else $seq = 1;
}
return $seq > 2;
}
$sum = 0;
for ($n=1; $n<=10000; $n++) {
if (isSequence3($n)) $sum += $n;
}
printf("Sum: %d", $sum);
printf(
"<p>Пользователь: %s %s</p>
<p><i>Контактные данные</i></p>
<p>E-mail: %s</p>
<p>Facebook: %s</p>
<p>Город: %s</p>
---------<br/>" // !!!!!!!!!!!! here
$row['first_name'], $row['last_name'], $row['email'], $row['facebook'], $row['otkuda']
);
<?php
$type = 'test';
$f2 = function($s) use($type) {echo $s . "<>" . $type; };
$f2('b');
-- using window functions
select distinct
uid, first_value(utm) over (partition by uid order by id desc) last_utm
from t;
-- using join
select t.uid, t.utm last_utm
from t
join (
select max(id) max_id from t group by uid
) max_ids on max_id = id;
-- using sub-query
select t.uid, t.utm last_utm
from t
where id = (select max(id) max_id from t group by uid having max(id) = t.id);
<?php
$random_string = strtoupper(bin2hex(random_bytes(12)));
echo $random_string;
$tags = array_column($val["tags"], "value");
echo implode(',', $tags);