<?php
$arrays = [
[
'foo' => 'bar',
'bar' => 'baz',
'baz' => 'foo',
],
[
'foo' => 'bar',
'baz' => 'foo',
],
[
'foo' => 'bar',
'bar' => 'baz',
],
];
function check_keys(array $input, array $keys)
{
return array_intersect($keys, array_keys($input)) === $keys;
}
var_dump(check_keys($arrays[0], ['foo', 'baz', 'bar']));
var_dump(check_keys($arrays[1], ['foo', 'baz']));
var_dump(check_keys($arrays[2], ['foo', 'bar']));
var_dump(check_keys($arrays[2], ['baz', 'bar']));
bool(true)
bool(true)
bool(true)
bool(false)
<?php
$data = <<<DOM
<div class="container">
<div class="catalog-list">
<div class="sub">
<div class="item"></div>
</div>
<div class="item"></div>
<div class="item"></div>
<div class="item" data-type="2"></div>
</div>
</div>
DOM;
$xml = new SimpleXMLElement($data);
$catalogListNodes = $xml->xpath('//*[@class="catalog-list"]/*');
$itemInSubNodes = $xml->xpath('//*[@class="sub"]/*[@class="item"]');
<?php
$inputs = [
[
['Пн', '08:00 - 20:00'],
['Вт', '08:00 - 20:00'],
['Ср', '08:00 - 20:00'],
['Чт', '08:00 - 20:00'],
['Пт', '08:00 - 20:00'],
['Сб', '08:00 - 18:00'],
['Вс', '10:00 - 15:00'],
],
[
['Пн', '08:00 - 19:00'],
['Вт', '08:00 - 19:00'],
['Ср', '08:00 - 21:00'],
['Чт', '08:00 - 20:00'],
['Пт', '08:00 - 20:00'],
['Сб', '08:00 - 18:00'],
['Вс', '10:00 - 15:00'],
],
];
function is_consecutive(array $numbers)
{
$count = count($numbers);
if ($count < 2) {
return false;
}
$first = $numbers[0];
$range = range($first, $first + $count - 1);
return $numbers === $range;
}
function format_day_times(array $input)
{
$timeIndexes = [];
foreach ($input as $index => list($day, $time)) {
$timeIndexes[$time][] = [$index, $day];
}
$result = [];
foreach ($timeIndexes as $time => $data) {
if (is_consecutive(array_column($data, 0))) {
$result[] = sprintf(
'%s-%s: %s',
$data[0][1],
array_slice($data, -1)[0][1],
$time
);
} else {
foreach ($data as list(, $day)) {
$result[] = sprintf('%s: %s', $day, $time);
}
}
}
return join(', ', $result);
}
foreach ($inputs as $input) {
echo format_day_times($input), PHP_EOL;
}
Пн-Пт: 08:00 - 20:00, Сб: 08:00 - 18:00, Вс: 10:00 - 15:00
Пн-Вт: 08:00 - 19:00, Ср: 08:00 - 21:00, Чт-Пт: 08:00 - 20:00, Сб: 08:00 - 18:00, Вс: 10:00 - 15:00
$in = '...your text...';
// never use "e"
$bad = preg_replace('/\&\#([0-9]+)\;/me', "((\\1>255)?(utf8_decode(code2utf(\\1))):('&#\\1;'))", $in);
// use callback instead
$good = preg_replace_callback(
'/\&\#([0-9]+)\;/m',
function ($m) {
if ($m[1] > 255) {
return utf8_decode(code2utf($m[1]));
}
return "&#${m[1]};";
},
$in
);
?
после +
. Откуда 010 + 1 = 9? Почему не 11?
Тот же самый вопрос, только тут получается 8
$a = 0123; // octal number (equivalent to 83 decimal)
Деление без остатка. В шестёрке четвёрка уберётся один раз, вроде бы 1 должно быть но ответ 2
Что за оператор =&
<?php
$input = [
[
'guid-1' => [
'one',
'two',
'three',
],
],
[
'guid-2' => [
'four',
],
],
];
var_dump(
array_reduce(
$input,
function ($acc, $item) {
foreach ($item as $guid => $values) {
$acc[$guid] = $values;
}
return $acc;
},
[]
)
);
array(2) {
["guid-1"]=>
array(3) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
string(5) "three"
}
["guid-2"]=>
array(1) {
[0]=>
string(4) "four"
}
}