3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $A = [3,45,67,8]; $B = [ 1 => [1,6,8], 2 => [5,67,3,4,5,66,6], 3 => [55,56,57,58], 4 => [45,80,81,82] ]; $result = []; foreach ($A as $needle) { if ($found = array_keys($B, $needle)) { // something qualified $result = array_merge($result, $found); // store the new key(s) $B = array_intersect($B, $found); // reduce the haystack } echo "Needle: $needle\n"; var_export($B); echo "\n--\n"; var_export($result); echo "\n----\n"; }
based on 1dPb1
Output for 5.6.38 - 7.3.0rc4
Needle: 3 array ( 1 => array ( 0 => 1, 1 => 6, 2 => 8, ), 2 => array ( 0 => 5, 1 => 67, 2 => 3, 3 => 4, 4 => 5, 5 => 66, 6 => 6, ), 3 => array ( 0 => 55, 1 => 56, 2 => 57, 3 => 58, ), 4 => array ( 0 => 45, 1 => 80, 2 => 81, 3 => 82, ), ) -- array ( ) ---- Needle: 45 array ( 1 => array ( 0 => 1, 1 => 6, 2 => 8, ), 2 => array ( 0 => 5, 1 => 67, 2 => 3, 3 => 4, 4 => 5, 5 => 66, 6 => 6, ), 3 => array ( 0 => 55, 1 => 56, 2 => 57, 3 => 58, ), 4 => array ( 0 => 45, 1 => 80, 2 => 81, 3 => 82, ), ) -- array ( ) ---- Needle: 67 array ( 1 => array ( 0 => 1, 1 => 6, 2 => 8, ), 2 => array ( 0 => 5, 1 => 67, 2 => 3, 3 => 4, 4 => 5, 5 => 66, 6 => 6, ), 3 => array ( 0 => 55, 1 => 56, 2 => 57, 3 => 58, ), 4 => array ( 0 => 45, 1 => 80, 2 => 81, 3 => 82, ), ) -- array ( ) ---- Needle: 8 array ( 1 => array ( 0 => 1, 1 => 6, 2 => 8, ), 2 => array ( 0 => 5, 1 => 67, 2 => 3, 3 => 4, 4 => 5, 5 => 66, 6 => 6, ), 3 => array ( 0 => 55, 1 => 56, 2 => 57, 3 => 58, ), 4 => array ( 0 => 45, 1 => 80, 2 => 81, 3 => 82, ), ) -- array ( ) ----