求高效率算法找

WBOY
Release: 2016-06-13 11:57:37
Original
1044 people have browsed it

求高效算法找

$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 数量不确定  
Copy after login

------解决方案--------------------
$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);
Copy after login
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 />
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template