$result = [];
foreach ($arr as $n) {
for ($visited = []; array_key_exists($n, $arr); $visited[] = $n, $n = $arr[$n]) {
if (($i = array_search($n, $visited)) !== false) {
$loop = array_slice($visited, $i);
if (empty(array_intersect(array_column($result, 0), $loop))) {
$result[] = $loop;
}
break;
}
}
}
$result = [];
$visited = [];
$iLoop = -1;
foreach ($arr as $n) {
for ($iLoop++; isset($arr[$n]); $visited[$n] = $iLoop, $n = $arr[$n]) {
if (isset($visited[$n])) {
if ($visited[$n] === $iLoop) {
for ($loop = [ $m = $n ]; ($m = $arr[$m]) !== $n; $loop[] = $m) ;
$result[] = $loop;
}
break;
}
}
}
$grouped = [];
foreach ($arr as [ 'id' => $id, 'name' => $name, 'key' => $key ]) {
$grouped[$key][$name] ??= [ 'name' => $name, 'ids' => [] ];
$grouped[$key][$name]['ids'][] = $id;
}
foreach ($keys as $k) {
$$k = array_map(fn($n) => [
'name' => $n['name'],
'ids' => implode(', ', $n['ids'])
], array_values($grouped[$k] ?? []));
}
function getCombinations($arr, $keys = [], $vals = []) {
return ($n = $arr[count($keys)] ?? null)
? array_merge(...array_map(
fn($k) => getCombinations(
$arr,
[ ...$keys, $k ],
[ ...$vals, ...$n[$k] ]
),
array_keys($n)
))
: [ implode('_', $keys) => $vals ];
}
function combine($ids, $keys, ...$values) {
return array_combine(
$ids,
array_map(
fn($i) => array_combine($keys, array_column($values, $i)),
array_keys($ids)
)
);
}
$result = combine($input, $params, $array1, $array2);
function createTree($data, $params = []) {
extract($params + [
'levelKey' => 'level',
'childrenKey' => 'children',
]);
$root = [];
foreach ($data as $n) {
$arr = &$root;
for (
$level = $data[0][$levelKey];
$level++ < $n[$levelKey];
$arr = &$arr[count($arr) - 1][$childrenKey]
) ;
$arr[] = $n + [ $childrenKey => [] ];
}
return $root;
}
$tree = createTree($arMenu, [ 'levelKey' => 'LEVEL' ]);
function createTree($data, $params = []) {
$levelKey = $params['levelKey'] ?? 'level';
$childrenKey = $params['childrenKey'] ?? 'children';
$root = [];
$stack = [ [ $data[0][$levelKey], &$root ] ];
foreach ($data as $n) {
$end = end($stack);
$level = $n[$levelKey];
if ($level > $end[0]) {
$stack[] = [ $level, &$end[1][count($end[1]) - 1][$childrenKey] ];
} else while ($level < end($stack)[0]) {
array_pop($stack);
}
end($stack)[1][] = array_merge($n, [ $childrenKey => [] ]);
}
return $root;
}
$extract = fn($keys, $item) => array_combine($keys, array_map(fn($k) => $item[$k], $keys));
$grouped = [];
$productKeys = [ 'product_id', 'sku', 'quantity' ];
$orderKeys = array_diff(array_keys($arr[0] ?? []), $productKeys);
foreach ($arr as $n) {
$id = $n['order_id'];
$grouped[$id] ??= $extract($orderKeys, $n);
$grouped[$id]['products'][] = $extract($productKeys, $n);
}
function merge($arrs, $idKey, ...$sumKeys) {
$result = [];
foreach (array_merge(...$arrs) as $n) {
$id = $n[$idKey];
$result[$id] ??= array_merge($n, array_combine($sumKeys, array_fill(0, count($sumKeys), 0)));
foreach ($sumKeys as $k) {
$result[$id][$k] += $n[$k];
}
}
return array_values($result);
}
$result = merge($dates1, 'date', 'qty');
$arr2 = array_combine(array_column($arr, 'sku'), $arr);
$result = array_map(fn($n) => $arr2[$n], $torgPred);
$result = array_filter($arr, fn($n) => in_array($n['sku'], $torgPred));
$value = array_combine($data['value_index'], $data['value'])[min($data['value_index'])];
$value = $data['value'][array_search(min($data['value_index']), $data['value_index'])];
$index = 0;
for ($i = 1; $i < count($data['value_index']); $i++) {
if ($data['value_index'][$i] < $data['value_index'][$index]) {
$index = $i;
}
}
$value = $data['value'][$index];
$values = array_column($array2, 'value');
$result = array_filter($array1, fn($n) => in_array($n->code, $values));
function getUrls($tree, $url = '') {
$urls = [];
foreach ($tree as $n) {
$newUrl = ($url ? $url.'/' : '').$n->url;
array_push($urls, $newUrl, ...getUrls($n->subcategories ?? [], $newUrl));
}
return $urls;
}
$toStr = fn($arr) => implode('', array_map(fn($k) => "[$k:]$arr[$k][:$k]", array_keys($arr)));
print_r(array_map($toStr, $lang));
почему ошибку выдает?
It should take an array of numbers as its argument and return the two highest numbers within the array.
function twoOldestAges($ages) {
sort($ages);
return array_slice($ages, -2);
}
function twoOldestAges($ages) {
$a = -INF;
$b = -INF;
foreach ($ages as $n) {
if ($n > $a) {
$b = $a;
$a = $n;
} else if ($n > $b) {
$b = $n;
}
}
return [ $b, $a ];
}
$newArr = array_filter($arr, fn($n) => explode('.', $n)[1] !== '08');
for ($i = 0, $j = 0; $i < count($arr); $i++) {
if (explode('.', $arr[$i])[1] === '08') {
$j++;
} else if ($j) {
$arr[$i - $j] = $arr[$i];
}
}
array_splice($arr, -$j, $j);
$minPrices = array_values(array_reduce($arr, function($acc, $n) {
$key = $n['code_id'];
if (!isset($acc[$key]) || $acc[$key]['price'] > $n['price']) {
$acc[$key] = $n;
}
return $acc;
}, []));