.
начало
---|---|---
к | 1
о -
н | 2 1
е -
ц | 3 2 1
if - else if - else
function ranges(int $a, int $b): array
{
$start = 20;
$finish = 30;
$result = [
'before' => null,
'match' => null,
'after' => null,
];
if ($a < $start) {
if ($b < $start) {
$result['before'] = [$a, $b];
} else {
$result['before'] = [$a, $start];
if ($b < $finish) {
$result['match'] = [$start, $b];
} else {
$result['match'] = [$start, $finish];
$result['after'] = [$finish, $b];
}
}
} else if ($a < $finish) {
if ($b < $finish) {
$result['match'] = [$a, $b];
} else {
$result['match'] = [$a, $finish];
$result['after'] = [$finish, $b];
}
} else {
$result['after'] = [$a, $b];
}
return $result;
}
print(json_encode(ranges(5, 25))); // {"before":[5,20],"match":[20,25],"after":null}
$arr = [$start, $finish, $a, $b];
sort($arr, SORT_NUMERIC);
1, 2, 3, 2, 3, 2, 1
0 + + - + - -
^ ^ ^ в этих точках знак поменялся
Складывать найденные точки в два массива: максимумы и минимумы.$str = "attachment=12345";
list($number) = sscanf($str, "attachment=%d");
echo $number; // 12345
rgb(0, 0, 0)
соответствует множество hsl(*, *, 0)
.$_POST['size']
статичную таблицу 3х3.$size = 3;
$rowTitles = explode(',', 'А,Б,В,Г,Д,Е,Ж,З,И,К');
$html = '';
for ($row = 0; $row <= $size; $row++) {
$tr = '';
for ($col = 0; $col <= $size; $col++) {
if ($row === 0) {
// строка с номерами столбцов
if ($col === 0) {
$tr .= '<th></th>'; // угловая пустая ячейка
} else {
$tr .= '<th>' . $col . '</th>'; // название столбца
}
} else {
// строка с обычными клеточками
if ($col === 0) {
// название строки
$tr .= '<th>' . $rowTitles[$row - 1] . '</th>';
} else {
// обычная клеточка
$tr .= '<td>' . $rowTitles[$row - 1] . $col . '</td>';
}
}
}
$html .= $tr . PHP_EOL;
}
$html = '<table><tbody>' . $html . '</tbody></table>';
echo $html;
$array = [
["TITLE" => "Тест 1", "LEVEL" => 1,],
["TITLE" => "Тест 2", "LEVEL" => 2,],
["TITLE" => "Тест 3", "LEVEL" => 2,],
["TITLE" => "Тест 4", "LEVEL" => 3,],
["TITLE" => "Тест 5", "LEVEL" => 2,],
["TITLE" => "Тест 6", "LEVEL" => 3,],
];
const TITLE = 'TITLE';
const LEVEL = 'LEVEL';
const CHILDREN = 'CHILDREN';
$result = [];
$refs = [];
foreach ($array as $item) {
$level = $item[LEVEL];
if ($level === 1) {
$result[] = $item;
$refs = [&$result[count($result) - 1]];
} else {
for ($i = count($refs) - 1; $i >= 0; $i--) {
$parent = &$refs[$i];
if ($parent[LEVEL] === $level - 1) {
if (!isset($parent[CHILDREN])) {
$parent[CHILDREN] = [];
}
$parent[CHILDREN][] = $item;
array_splice($refs, $i + 1);
$refs[] = &$parent[CHILDREN][count($parent[CHILDREN]) - 1];
unset($parent);
break;
} else {
unset($parent);
continue;
}
}
}
}
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
[
{
"TITLE": "Тест 1",
"LEVEL": 1,
"CHILDREN": [
{
"TITLE": "Тест 2",
"LEVEL": 2
},
{
"TITLE": "Тест 3",
"LEVEL": 2,
"CHILDREN": [
{
"TITLE": "Тест 4",
"LEVEL": 3
}
]
},
{
"TITLE": "Тест 5",
"LEVEL": 2,
"CHILDREN": [
{
"TITLE": "Тест 6",
"LEVEL": 3
}
]
}
]
}
]
$recentChannel = null;
foreach ($xml->channel->item as $channel) {
if (!$recentChannel || strtotime($channel->pubDate) > strtotime($recentChannel->pubDate)) {
$recentChannel = $channel;
}
}
// теперь $recentChannel содержит канал с самой свежей датой
$str = "0"; // непустая строка, содержащая цифру ноль
if (!$str) echo "bool false\n"; // сработает
if (empty($str)) echo "is empty\n"; // сработает
$str = null; // не строка
if ($str == "") echo "equals empty str\n"; // сработает
if (strlen($str) === 0) echo "zero length str\n"; // сработает
===
с пустой строкой.