我需要过滤我的php数组并使其独一无二。 这是我的阵列:
array (size=50)
0 =>
array (size=1)
'circuit_reference' => string '1' (length=1)
1 =>
array (size=2)
'circuit_reference' => string '1' (length=1)
'circuit_phase' => string 'L1' (length=2)
2 =>
array (size=3)
'circuit_reference' => string '1' (length=1)
'circuit_phase' => string 'L1' (length=2)
'circuit_is_sub_main' => string 'N' (length=1)
3 =>
array (size=1)
'circuit_reference' => string '1' (length=1)
4 =>
array (size=2)
'circuit_reference' => string '1' (length=1)
'circuit_phase' => string 'L2' (length=2)
我需要在新数组中只有具有最大计数元素的子数组:
array(
1 =>
array (size=3)
'circuit_reference' => string '1' (length=1)
'circuit_phase' => string 'L1' (length=2)
'circuit_is_sub_main' => string 'N' (length=1)
2 =>
array (size=2)
'circuit_reference' => string '1' (length=1)
'circuit_phase' => string 'L2' (length=2)
坚持回答(
答案 0 :(得分:1)
我自己解决了这个问题,这里是答案:
foreach ($arrays as $array) {
if (isset($array['circuit_reference']) && isset($array['circuit_phase'])) {
if ($curr == '' && $curt == '') {
$curr = $array['circuit_reference'];
$curt = $array['circuit_phase'];
}
if ($curr != $array['circuit_reference'] || $curt != $array['circuit_phase']) {
$curr = $array['circuit_reference'];
$curt = $array['circuit_phase'];
$resulted_circuits[] = $arrays[$maxin];
$countm = 0;
$maxin = 0;
}
if ($countm < count($array)) {
$countm = count($array);
$maxin = $i;
}
}
$i++;
}
if ($maxin != 0 && $maxin <= count($arrays)) {
$resulted_circuits[] = $arrays[$maxin];
}
感谢大家。
答案 1 :(得分:0)
试试这个
$arrays = array(
array('circuit_reference' => '1'),
array('circuit_reference' => '1', 'circuit_phase' => 'L1'),
array('circuit_reference' => '1', 'circuit_phase' => 'L1','circuit_is_sub_main' => 'N'),
array('circuit_reference' => '1'),
array('circuit_reference' => '1', 'circuit_phase' => 'L2'),
array('circuit_reference' => '1', 'circuit_phase' => 'L2','circuit_is_sub_main' => 'N' )
);
$max = count(max($arrays));
foreach($arrays as $arr)
{
if(count($arr) == $max)
$arrays1[] = $arr;
}
var_dump($arrays1);
out put
array(2) {
[0]=>
array(3) {
["circuit_reference"]=>
string(1) "1"
["circuit_phase"]=>
string(2) "L1"
["circuit_is_sub_main"]=>
string(1) "N"
}
[1]=>
array(3) {
["circuit_reference"]=>
string(1) "1"
["circuit_phase"]=>
string(2) "L2"
["circuit_is_sub_main"]=>
string(1) "N"
}
}