Search for efficient algorithms
$ A = array ('a' => array (,), 'B' => array (,), 'C' => array, 7, 8, 9),) $ B = array ('2', '4 ') find out the subset of the $ B array that belongs to the array $ a, B, and C by using an efficient algorithm. Note that the number of $ A and $ B is uncertain.
Reply to discussion (solution)
$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; }}
I don't know. this is not efficient.
$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 ))
NIUBI ..