假设所有这些都有相同的数据。
我将如何在这个多维数组中做到这一点。
我基本上试图使用
array_intersect($array,$array);
但我无法弄清楚我应该做什么才能做到这一点。
array(14) {
[0]=>
string(5) "0.1"
[1]=>
string(5) "0.2"
[2]=>
string(5) "0.3"
[3]=>
string(5) "0.4"
[4]=>
string(5) "0.1"
[5]=>
string(5) "0.2"
[6]=>
string(5) "0.3"
[7]=>
string(5) "0.4"
[8]=>
string(5) "0.2"
[9]=>
string(5) "0.3"
[10]=>
string(5) "0.4"
[11]=>
string(5) "0.1"
[12]=>
string(5) "0.2"
[13]=>
string(5) "0.3"
}
不确定您到底在寻找什么:
$i=0; // index of the orginal array
$common = array_reduce($array, function ($c, $v) use (&$i) {
foreach ($v['data'] as $nb) {
$c[$nb][] = $i; // store the index for the current number
}
$i++; // next index
return $c;
}, array());
// remove the unique values
$common = array_filter($common, function($item) { return count($item)>1; });
// sort by decreasing number of indexes
uasort($common, function ($a, $b) {return count($b) - count($a);});
print_r($common);
这将生成一个以公共数字作为键的关联数组,以及一个以原始数组的索引作为值的数组:
Array
(
[5] => Array (
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
[4] => Array (
[0] => 0
[1] => 1
[2] => 3
)
[2] => Array (
[0] => 0
[1] => 1
[2] => 3
)
[6] => Array (
[0] => 1
[1] => 3
)
[1] => Array (
[0] => 1
[1] => 3
)
[3] => Array (
[0] => 1
[1] => 3
)
)