Maison > Article > développement back-end > 求高效率算法找
求高效算法找
$a = array(<br /> 'A' => array(1,2,3,4,5,6),<br /> 'B' => array(1,7,8,9),<br /> 'C' => array(1,6,7,8,9),<br />)<br /> <br /> <br />$b = array(<br /> '2','4'<br />)<br /> 求高效算法找出 $b数组 属于数组 $a A、B、C的那一项的子集,注 $a,$b 数量不确定
$a = array(<br /> 'A' => array(1,2,3,4,5,6),<br /> 'B' => array(1,7,8,9),<br /> 'C' => array(1,6,7,8,9),<br />);<br /> <br />$b = array(<br /> '2','4'<br />);<br /><br />$r = array_filter($a, function($t) use ($b) {<br /> return array_intersect($b, $t) == $b;<br />});<br /><br />print_r($r);
Array<br />(<br /> [A] => Array<br /> (<br /> [0] => 1<br /> [1] => 2<br /> [2] => 3<br /> [3] => 4<br /> [4] => 5<br /> [5] => 6<br /> )<br /><br />)<br /><br />