让我们说所有这些都有相同的数据。
我将如何在这个多维数组中执行此操作。
我基本上试图使用
找到所有相同的元素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"
}
答案 0 :(得分:0)
不确定您到底在寻找什么:
$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
)
)