原文地址
<?php
$array = [1, 2, 3, 4, 5, 6, 7, 8];
$lists = [
'a' => [7, 8, 6, 2, 4],
'b' => [7, 1, 2, 3],
'c' => [4, 5, 6, 8],
'd' => [6, 7, 8, 1],
'e' => [3, 5, 8]
];
$items = [];
foreach ($array as $key => $value) {
$it = [
'value' => $value,
'list' => [],
];
foreach ($lists as $kk => $vv) {
if (in_array($value, $vv)) {
$it['list'][] = $kk;
}
}
$items[] = $it;
}
$find = [];
$i = 0;
$allCount = count($items);
foreach ($items as $ki => $vi) {
$before = [];
if ($allCount != $i) {
$before = $find;
}
$find = [];
if ($i == 0) {
foreach ($vi['list'] as $kik => $viv) {
$isss = $viv . '-' . $vi['value'];
$find[] = $isss;
}
}
if ($i == 1) {
foreach ($vi['list'] as $kik => $viv) {
$isss = $viv . '-' . $vi['value'];
foreach ($before as $k1 => $v1) {
$find[] = [$v1, $isss];
}
}
}
if ($i > 1) {
foreach ($vi['list'] as $kik => $viv) {
$isss = $viv . '-' . $vi['value'];
foreach ($before as $k2 => $v2) {
$kss = [$isss];
foreach ($v2 as $k3 => $v3) {
$kss[] = $v3;
}
$find[] = $kss;
}
}
}
$i++;
}
$ress = [];
foreach ($find as $kf => $vf) {
$item = [];
foreach ($vf as $k3 => $v3) {
$ks3 = explode('-', $v3);
$item[$ks3[0]][] = $ks3[1];
}
$ress[] = $item;
}
var_dump($ress);
本作品採用《CC 協議》,轉載必須註明作者和本文連結