<?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)
// store
const initalState = {
treesById: {},
activeByTreeId: {},
};
const treesReducer = (state, action) => {
switch (action.type) {
// store.dispatch({ type: 'ADD_TREE', id: 'users', tree: [...] })
case 'ADD_TREE':
return {
...state,
treesById: {
...state.treesById,
[action.id]: action.tree,
},
};
// store.dispatch({ type: 'SET_ACTIVE', id: 'users', value: 5 })
case 'SET_ACTIVE':
return {
...state,
activeByTreeId: {
...state.activeByTreeId,
[action.id]: action.value,
},
};
default:
return state;
}
};
// dumb component aka presenter
const Tree = ({ items, active }) => {
return (
<div className="tree-items">
{items.map((item) => (
<TreeItem
key={item.id}
item={item}
isActive={item.id === active}
/>
))}
</div>
);
};
// smart component aka container
// (state, ownProps)
const mapStateToProps = ({ trees }, { treeId }) => {
return {
items: trees.treesById[treeId] || [],
active: trees.activeByTreeId[treeId],
};
};
// use in any component, e.g. Sidebar (presenter)
// props are passed from container
const Sidebar = () => {
return (
<div className="sidebar">
<TreeContainer treeId="sidebar" />
</div>
);
};
<?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