Heim  >  Artikel  >  Backend-Entwicklung  >  求高效算法找

求高效算法找

WBOY
WBOYOriginal
2016-06-23 13:56:52951Durchsuche

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


回复讨论(解决方案)

$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);
Array(    [A] => Array        (            [0] => 1            [1] => 2            [2] => 3            [3] => 4            [4] => 5            [5] => 6        ))

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

不知道这个算不算高效

$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);
Array(    [A] => Array        (            [0] => 1            [1] => 2            [2] => 3            [3] => 4            [4] => 5            [5] => 6        ))

牛比。。
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:求大侠帮忙Nächster Artikel:数组操作问题