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

Link to comment
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 ;)

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.