let hours = [
{"hour":0, "car":0, "bike": 0},
{"hour":1, "car":0, "bike": 0},
{"hour":2, "car":0, "bike": 0},
{"hour":3, "car":0, "bike": 0},
{"hour":4, "car":0, "bike": 0},
{"hour":5, "car":0, "bike": 0},
{"hour":6, "car":0, "bike": 0},
{"hour":7, "car":0, "bike": 0}
];
let first = [
{"hour":5, "car":10, "bike": 10},
{"hour":8, "car":20, "bike": 0},
{"hour":9, "car":30, "bike": 0},
{"hour":10, "car":40, "bike": 0}
];
let second = [
{"hour":0, "car":11, "bike": 12},
{"hour":7, "car":15, "bike": 25},
{"hour":8, "car":20, "bike": 30},
{"hour":9, "car":10, "bike": 30},
{"hour":10, "car":120, "bike": 140}
];
function mergeArraysOfObjects(source, recepient, uniqueKey) {
var result = [];
source.forEach(function(sourceElement) {
var foundRecepientElement = null;
recepient.forEach(function(recepientElement) {
if (recepientElement[uniqueKey] === sourceElement[uniqueKey]) {
foundRecepientElement = recepientElement;
}
});
if (foundRecepientElement !== null) {
result.push(foundRecepientElement);
} else {
result.push(sourceElement);
}
});
recepient.forEach(function(recepientElement) {
var foundSourceElement = null;
source.forEach(function(sourceElement) {
if (sourceElement[uniqueKey] === recepientElement[uniqueKey]) {
foundSourceElement = sourceElement;
}
});
if (foundSourceElement === null) {
result.push(recepientElement);
}
});
return result;
}
var firstMerge = mergeArraysOfObjects(hours, first, 'hour');
var secondMerge = mergeArraysOfObjects(firstMerge, second, 'hour');
function groupBy(array $elements, callable $getUniqueKey) {
$grouped = [];
foreach ($elements as $element) {
$uniqueKey = $getUniqueKey($element);
$grouped[$uniqueKey][] = $element;
}
return $grouped;
}
$groupedByDate = groupBy($app, function($event) {
return date("Y-m-d", strtotime($event['created_at']));
});
$strings = [
'1st, 2nd, 535',
'2nd, 2nd, 13',
'3rd, 2nd, 442',
];
function findString($number, array $strings, $criterionStart=1) {
$found = null;
$criterionEnd = $criterionStart;
foreach ($strings as $string) {
list(2=>$criterionIncrement) = explode(', ', $string);
$criterionEnd += $criterionIncrement;
if ($number>=$criterionStart && $number<$criterionEnd) {
$found = $string;
break;
}
$criterionStart = $criterionEnd;
}
return $found;
}
findString(rand(1,990), $strings);
Уверен, что задача простая, но я никак не соображу.
Предположим есть следующая структура в таблице БД.
Users
1.id
2.name
3.affiliated_by
function buildAffiliateChain($for, $users) {
$chain = [];
$affiliatedUsers = array_values(array_filter($users, function($user) use ($for) {
return $user['affiliated_by'] === $for;
}));
foreach ($affiliatedUsers as $affiliatedUser) {
$subscequentChain = buildAffiliateChain($affiliatedUser['id'], $users);
$chain = array_merge($chain, [$affiliatedUser], $subscequentChain);
}
usort($chain, function($someUser, $otherUser) {
return $someUser['affiliated_by'] > $otherUser['affiliated_by'] ? 1 : -1;
});
return $chain;
}
А ведь не должен приходить, я же не нажимал кнопку "Отправить"
<?php if (get_page_by_path("page-slug")->ID == get_the_ID()) { ?>
Мы на конкретной странице
<?php } ?>
$text = "тут мой текст";
$stopWords = explode(PHP_EOL, file_get_contents("stop-words.txt"));
$message = "готово":
foreach (explode(" ", $text) as $word) {
if (in_array($word, $stopWords)) {
$message = "Ошибка";
break;
}
}
echo $message;
SELECT
`product`.*,
SUM(`pay`.`sum`)/`product`.`price`*100 AS 'percent' -- исправьте эту строку, применив правильную формулу.
FROM
`product`
JOIN `pay` ON `product`.`paysystem_id` = `pay`.`pay_id`
GROUP BY `product`.`id`
<?php foreach ($products as $product): ?>
<?= $product['percent'] ?>
<?php endforeach; ?>
Только остался вопрос, почему этот код в phpmyadmin не работает?
<input name="rows[0][value]">
<input name="rows[1][value]">
<input name="rows[2][value]">
$rows = $_POST['rows'];
foreach ($rows as $row) {
$value = $row['value'];
/* Здесь делайте с $value что хотите */
}
</php>