function getProperties(array $haystack, string $needle): array
{
$result = [];
foreach ($haystack as $key => $item) {
if ($key === $needle) {
$result[] = $item;
} elseif (is_array($item)) {
$result = array_merge($result, getProperties($item, $needle));
}
}
return $result;
}
'products': <?= json_encode($products, JSON_UNESCAPED_UNICODE) ?>,
<?php
function prepareDataForDashboard(array $data, DateTimeInterface $minDate, DateTimeInterface $maxDate): array {
$dateInterval = new DateInterval('P1D');
$datePeriod = new DatePeriod($minDate, $dateInterval, $maxDate);
$result = [];
foreach ($datePeriod as $item) {
$date = $item->format('m-Y');
$result[$date] = [
'date' => $date,
'name' => null,
];
}
foreach ($data as $item) {
$date = DateTimeImmutable::createFromFormat('m-Y', $item['date']);
$date = $date->format('m-Y');
$result[$date] = $item;
}
return array_values($result);
}
$data = [
['date' => '06-2020', 'name' => 'Vanaya'],
['date' => '01-2020', 'name' => 'Petya'],
['date' => '04-2019', 'name' => 'Grizha'],
['date' => '11-2019', 'name' => 'Oleg'],
];
$minDate = new DateTimeImmutable('01.04.2019');
$maxDate = new DateTimeImmutable('01.06.2020');
$result = prepareDataForDashboard($data, $minDate, $maxDate);
print_r($result);
if (R::findOne('favorite', ' favorite = ? AND user_id = ?', [$bookName, $userId])) {
echo "такая книга уже есть";
}
SourceQuery::GOLDSOURCE
SourceQuery::SOURCE
$var1 = '9995КНМ';
$var2 = '9996КНМ';
function check(string $var1, string $var2): array
{
$matches = [];
$length = mb_strlen($var2);
for ($i = 0; $i < $length; $i++) {
for ($j = 1; $j < $length - $i + 1; $j++) {
$substr = mb_substr($var2, $i, $j);
$substrLength = mb_strlen($substr);
$substrCount = mb_substr_count($var1, $substr);
if ($substrCount) {
$matches[$substr] = [
'substr' => $substr,
'length' => $substrLength,
'count' => $substrCount,
];
}
}
}
return array_values($matches);
}
$matches = check($var1, $var2);
Array
(
[0] => Array
(
[substr] => 9
[length] => 1
[count] => 3
)
[1] => Array
(
[substr] => 99
[length] => 2
[count] => 1
)
[2] => Array
(
[substr] => 999
[length] => 3
[count] => 1
)
[3] => Array
(
[substr] => К
[length] => 1
[count] => 1
)
[4] => Array
(
[substr] => КН
[length] => 2
[count] => 1
)
[5] => Array
(
[substr] => КНМ
[length] => 3
[count] => 1
)
[6] => Array
(
[substr] => Н
[length] => 1
[count] => 1
)
[7] => Array
(
[substr] => НМ
[length] => 2
[count] => 1
)
[8] => Array
(
[substr] => М
[length] => 1
[count] => 1
)
)
<?php
$numbers1 = [-5, 10, 0, 1, 2, 3, 6, 7, 8, 9, 12, 13, 14, 15];
$numbers2 = [10, 0, 1, 2, 3, 6, 7, 8, 9, 12, 13, 14, 15];
function intervals(array $numbers): array
{
$numbers = array_unique($numbers);
sort($numbers);
$length = count($numbers);
$intervals = [];
$start = null;
$end = null;
for ($i = 0; $i < $length; $i++) {
$currentItem = $numbers[$i];
$nextItem = $numbers[$i + 1] ?? null;
if ($currentItem + 1 === $nextItem) {
if ($start === null) {
$start = $currentItem;
}
$end = $nextItem;
continue;
}
if ($start === null || $end === null) {
continue;
}
$intervals[] = [
'start' => $start,
'end' => $end,
];
$start = $nextItem;
}
return $intervals;
}
print_r(intervals($numbers1));
print_r(intervals($numbers2));
/**
* Class WebCategory
*
* @mixin Category
*/
class WebCategory
{
private $cat;
public function __construct(Category $cat)
{
$this->cat = $cat;
}
public function __call($name, $arguments)
{
return $this->cat->$name($arguments);
}
}
$string = 'ips|127.0.0.1||port|12871||query|12881|';
[, $ip, , , $port, , , $query] = explode('|', $string);
var_dump($ip);
var_dump($port);
var_dump($query);
echo "<strong>Фотографии успешно загружены!</strong>";
<?php
$list = [
'ЦАО',
'САО',
'СВАО',
'СЗАО',
'ЗАО',
'ВАО',
'ЮАО',
'ЮЗАО',
'ЮВАО',
'ТиНАО',
'ЗелАО',
];
?>
<select name='district' class='custom-select'>
<?php foreach ($list as $item): ?>
<option value='<?= $item ?>' <?= $item === $row[0]['district'] ? 'selected' : '' ?>>
<?= $item ?>
</option>
<?php endforeach; ?>
</select>