Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8216709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:13:30+00:00 2026-06-07T12:13:30+00:00

I am looping through an associative array and trying to match the whole word

  • 0

I am looping through an associative array and trying to match the whole word from the needle array into the haystack array. The following code works perfectly but returns too many results since it’s matching any occurance of the string:

$result = array();
$name1 = array();
$name2 = array();
foreach($object->organizations as $o) {
    foreach($objectCrunch as $o2) {
        $name1 = $o->name;
        $name2 = $o2->name;

        if(stristr($name2, $name1)) {
            $result[] = $o2->permalink;
        }
    }
}

When I try the following code:

$result = array();
$name1 = array();
$name2 = array();
foreach($object->organizations as $o) {
    foreach($objectCrunch as $o2) {
        $name1 = $o->name;
        $name2 = $o2->name;

        $pattern = "'/\b" . $name2 . "\b/i'";

        if(preg_match($pattern, $name1)) {
            $result[] = $o2->permalink;
        }
    }
}

I get an empty array. Any help getting the last piece of code working would be great.

Thanks,
Greg

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:13:31+00:00Added an answer on June 7, 2026 at 12:13 pm

    You are using two pairs of delimiters here:

    $pattern = "'/\b" . $name2 . "\b/i'";
                ||      
                1|
                 2
    

    Use either / or '.
    I’d say the single quotes are redundant and the result of hasty copy & pasting.

    Also, preg_quote for the content/word if it might contain either.


    So as Elias points out; The next pitfall is likely trying to use the long content variable as search parameter. This would have been an obvious error, if you didn’t give your variables meaningless enumerated names.

    Only one of them is likely an actual word or name. The other should be called content or something:

        $word = $o->name;
        $textcontent = $o2->name;
    

    Use those henceforth.
    While at it, also fix the object property naming accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looping through an associative array with a foreach. I'd like to be able
Looping through each id retrieve value of objects in 1 dimentional array and 2
While looping through a Dataset which code snippet should I use, should I go
Must be a way looping through this code: private void loadSprites() { this.sprites[0] =
I have an associative array and while iterating through this array, using foreach loop.
I have a Mysql Recordset that I have put into an associative array so
Simple, quick, question. How can i pass a hash or associative array through my
I was looking through some code from a firefox extension (here: https://github.com/mozilla/prospector/blob/master/oneLiner/bootstrap.js#L34 ) and
When looping through a DataRow and encountering types such as DataRow dr; dr[someString] dr[someInteger]
I've been looping through the contents of a list in the simple fashion to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.