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

  • SEARCH
  • Home
  • 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 3211146
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:41:36+00:00 2026-05-17T14:41:36+00:00

I am writing a very process-intensive function in PHP that needs to be as

  • 0

I am writing a very process-intensive function in PHP that needs to be as optimized as it can get for speed, as it can take up to 60 seconds to complete in extreme cases. This is my situation:

I am trying to match an array of people to an XML list of jobs. The array of people have keywords that I have already analyzed, delimited by spaces. The jobs are from a large XML file.

It’s currently setup like this:

$matches = new array();
foreach($people as $person){
    foreach($jobs as $job){
        foreach($person['keywords'] as $keyword){
            $count = substr_count($job->title, $keyword);
            if($count > 0) $matches[$job->title] = $count;
        }
    }
}

I do the keywords loop a few times with different categories. It does what I need it to do, but it feels very sloppy and the process can take a very, very long time depending on the number of people/jobs.

Is there a more efficient, or faster, way of doing this?

  • 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-05-17T14:41:36+00:00Added an answer on May 17, 2026 at 2:41 pm
    $matches = new array();
    foreach($people as $person){
        foreach($jobs as $job){
            foreach($person['keywords'] as $keyword){
                $count = substr_count($job->title, $keyword);
                if($count > 0) $matches[$job->title] = $count;
            }
        }
    }
    

    Truthfully, your method is a bit sloppy, but I assume that’s because you have some specially formatted data that you have to work around? Although other than just being sloppy, I see a bit of lost data in the way you’re processing things that I don’t think was intentional.

    I see that you’re not just checking “is the keyword in the job title”, but “how many times is the keyword in the job title” and then you’re storing this. This means for the job title friendly friend of the friend company, the “keyword” friend shows up 3 times, and thus $matches["friendly friend of the friend company"] = 3. Since you’re declaring $matches before you being your $people foreach loop, though, this means you keep over-writing this value any time a new person has that keyword. In other words, if the first person has the keyword “friend” then $matches["friendly friend of the friend company"] is set to 3. Then if the second person has the keyword “friendly”, this value is over-written and $matches["friendly friend of the friend company"] now equals 1.

    I think what you wanted to do was count how many people have a keyword which is contained in the job title. In this case, rather than counting how many times $keyword appears in $job->title, you should just see if it appears, and respond accordingly.

    $matches = new array();
    foreach($people as $person){
        foreach($jobs as $job){
            foreach($person['keywords'] as $keyword){
                if(strpos($job->title, $keyword) !== FALSE) /* "If $keyword exists in $job->title" */
                    $matches[$job->title]++; /* Increment "number of people who match" */
            }
        }
    }
    

    Another possibility is that you wanted to know how many keywords a given person had which matched a given job title. In this case you’d want a separate array per person. This is done with a slight modification.

    $matches = new array();
    foreach($people as $person){
        $matches[$person] = new array();
        foreach($jobs as $job){
            foreach($person['keywords'] as $keyword){
                if(strpos($job->title, $keyword) !== FALSE) /* "If $keyword exists in $job->title" */
                    $matches[$person][$job->title]++; /* Increment "number of keywords which match" */
            }
        }
    }
    

    Or, alternatively, you could return to counting how many times a keyword matches now since per-person this is actually a meaningful value (“how well does the job match”)

    $matches = new array();
    foreach($people as $person){
        $matches[$person] = new array();
        foreach($jobs as $job){
            foreach($person['keywords'] as $keyword){
                if($count = substr_count($job->title, $keyword)) /* if(0) = false */
                    $matches[$person][$job->title] += $count; /* Increase "number of keywords which match" by $count */
            }
        }
    }
    

    Essentially, before tackling the problem of making your loop for efficient, you need to figure out what it is your loop is really trying to accomplish. Figure this out and then your best bet for increasing the efficiency is to just decrease the number of iterations of the loop to a minimum and use as many built-in functions as possible since these are implemented in C (a non-interpreted and therefore quicker-running language).

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

Sidebar

Related Questions

I am writing a very specialized app in C# that floats as a mostly
When it comes to writing custom MySQL database-driven PHP session management for a VERY
I've been writing PHP web applications for some time, and have come across very
I'm working on a piece of scientific software that is very cpu-intensive (its proc
I'm currently writing a process that generates Powerpoint reports programmatically from a given template
I have a scenario where one PHP process is writing a file about 3
I'm writing a very simple script to control a process, script checks if the
I'm writing a TCP server that functions very much like a chatroom and came
I am in the process of re-writing some very outdated .NET 2.0 SOAP web
I am writing a (very small) framework for checking pre- and postconditions of methods.

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.