class Mutation
{
private $len = 0;
private $arr = [];
private $table = [];
private $result = [];
public function __construct(array $arr, int $len)
{
$this->len = $len;
$this->setHashTable($arr);
$this->arr = array_unique($arr);
}
private function setHashTable(array $arr)
{
foreach ($arr as $value) {
isset($this->table[$value]) ? $this->table[$value] += 1 : $this->table[$value] = 1;
}
}
private function build()
{
$this->result = array_reduce(array_fill(0, $this->len - 1, $this->arr), function ($previous, $current) {
$result = [];
foreach ($current as $a) {
foreach ($previous as $b) {
if (is_array($b)) {
$values = array_count_values($b);
if (empty($values[$a]) || $this->table[$a] > $values[$a]) {
$result[] = array_merge([$a], $b);
}
} elseif ($a !== $b || $this->table[$a] > 1) {
$result[] = array_merge([$a], [$b]);
}
}
}
return $result;
}, $this->arr);
}
public function print()
{
empty($this->result) && $this->build();
print_r($this->result);
}
public function getCount()
{
empty($this->result) && $this->build();
return count($this->result);
}
}
$mutation = new Mutation([1, 2, 3], 2);
$mutation->print();
echo $mutation->getCount();
$json = '{"ops":[{"url":"site.com/100/test"},{"url":"site.com/200/test"}]}';
$json = str_replace([100, 200], [1000, 2000], $json);
echo $json;
//{"ops":[{"url":"site.com/1000/test"},{"url":"site.com/2000/test"}]}
include(dirname(__FILE__) . "/item/index.php?id=".$routes[1]);
<?php
# Author - Fedor Vlasenko, vlasenkofedor@gmail.com
define('METHOD', $_SERVER['REQUEST_METHOD']);
define('URI', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
function router($url, ...$args)
{
(empty($args[1]) || false !== strpos(METHOD, $args[0]))
&& (URI === $url || preg_match('#^' . $url . '$#iu', URI, $match))
&& die(call_user_func_array(end($args), $match ?? []));
}
router('/api/games', 'GET', function () {
echo 'список игрушек';
});
router('/api/game/(\d+)', 'GET', function (...$args) {
echo 'информация о игрушке: ', $args[1];
});
router('/api/games', 'POST', function () {
echo 'добавить новую игрушку';
});
router('/api/games/(\d+)', 'PUT', function (...$args) {
echo 'обновить существующую игрушку: ', $args[1];
});
router('/api/games/(\d+)', 'DELETE', function (...$args) {
echo ' удалить игрушку: ', $args[1];
});
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
echo '404';