Jump to content

preg_match two arrays.... help?


jdubwelch

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/173175-preg_match-two-arrays-help/
Share on other sites

$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 ;)

$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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.