jdubwelch Posted September 4, 2009 Share Posted September 4, 2009 I have two arrays... $commonFields = array ('bed','bath','school'); $fieldNames = array ('Lisitng File #', 'Chain Y/N', 'Bedrooms', 'Half Baths', 'School District'); foreach ($fieldNames as $field) { // Find match in $commonFields } I was thinking using in_array but i need something more i need it to out put: Bedrooms, Half Baths, and School District Quote Link to comment https://forums.phpfreaks.com/topic/173175-preg_match-two-arrays-help/ Share on other sites More sharing options...
samshel Posted September 4, 2009 Share Posted September 4, 2009 $commonFields = array ('bed','bath','school'); $fieldNames = array ('Lisitng File #', 'Chain Y/N', 'Bedrooms', 'Half Baths', 'School District'); $arrMatches = array(); foreach ($fieldNames as $field) { foreach ($commonFields as $commonfield) { if(eregi($commonfield, $field)) $arrMatches[] = $field; } } print_r($arrMatches); not tested. Also i assume u must have thought about this and were looking for a shorter way, but still trying Quote Link to comment https://forums.phpfreaks.com/topic/173175-preg_match-two-arrays-help/#findComment-912816 Share on other sites More sharing options...
kratsg Posted September 5, 2009 Share Posted September 5, 2009 $commonFields = array ('bed','bath','school'); $fieldNames = array ('Lisitng File #', 'Chain Y/N', 'Bedrooms', 'Half Baths', 'School District'); $arrMatches = array(); foreach ($fieldNames as $field) { foreach ($commonFields as $commonfield) { if(eregi($commonfield, $field)) $arrMatches[] = $field; } } print_r($arrMatches); not tested. Also i assume u must have thought about this and were looking for a shorter way, but still trying Two things: I would not suggest using 'eregi' anymore (as it appears later versions of PHP will move towards POSIX, "preg"). Second, when using eregi in this case, you still need the delimiters around the pattern to match. $commonFields = array ('bed','bath','school'); $fieldNames = array ('Lisitng File #', 'Chain Y/N', 'Bedrooms', 'Half Baths', 'School District'); $arrMatches = array(); foreach ($fieldNames as $field) { foreach ($commonFields as $commonfield) { if(preg_match("/$commonfield/i", $field))//note the difference, the "i" at the end is for case-insensitive matches $arrMatches[] = $field; } } print_r($arrMatches); Quote Link to comment https://forums.phpfreaks.com/topic/173175-preg_match-two-arrays-help/#findComment-912836 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.