求高效算法找

WBOY
Release: 2016-06-23 13:56:52
Original
951 people have browsed it

$a = array(    'A' => array(1,2,3,4,5,6),    'B' => array(1,7,8,9),    'C' => array(1,6,7,8,9),)        $b = array(    '2','4')    求高效算法找出 $b数组  属于数组 $a  A、B、C的那一项的子集,注 $a,$b 数量不确定  
Copy after login


回复讨论(解决方案)

$a = array(    'A' => array(1,2,3,4,5,6),    'B' => array(1,7,8,9),    'C' => array(1,6,7,8,9),);     $b = array(    '2','4');$r = array_filter($a, function($t) use ($b) {  return array_intersect($b, $t) == $b;});print_r($r);
Copy after login
Copy after login
Array(    [A] => Array        (            [0] => 1            [1] => 2            [2] => 3            [3] => 4            [4] => 5            [5] => 6        ))
Copy after login
Copy after login

foreach ($a as $key => $aa) {    if (count($aa) == count(array_flip(array_merge($aa, $b)))) {        echo $key;        break;    }}
Copy after login

不知道这个算不算高效

$a = array(    'A' => array(1,2,3,4,5,6),    'B' => array(1,7,8,9),    'C' => array(1,6,7,8,9),);     $b = array(    '2','4');$r = array_filter($a, function($t) use ($b) {  return array_intersect($b, $t) == $b;});print_r($r);
Copy after login
Copy after login
Array(    [A] => Array        (            [0] => 1            [1] => 2            [2] => 3            [3] => 4            [4] => 5            [5] => 6        ))
Copy after login
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!