Home >Backend Development >PHP Tutorial >求高效率算法找

求高效率算法找

WBOY
WBOYOriginal
2016-06-13 11:57:371070browse

求高效算法找

$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 />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn