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 8828423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:36:12+00:00 2026-06-14T07:36:12+00:00

I can do this, I’m just wondering if there is a more elegant solution

  • 0

I can do this, I’m just wondering if there is a more elegant solution than the 47 hacked lines of code I came up with…

Essentially I have an array (the value is the occurrences of said string);

[Bob] => 2
[Theresa] => 3
[The farm house] => 2
[Bob at the farm house] => 1

I’d like to iterate through the array and eliminate any entries that are sub-strings of others so that the end result would be;

[Theresa] => 3
[Bob at the farm house] => 1

Initially I was looping like (calling this array $baseTags):

foreach($baseTags as $key=>$count){
   foreach($baseTags as $k=>$c){
      if(stripos($k,$key)){
            unset($baseTags[$key]);
      }
   }
}

I’m assuming I’m looping through each key in the array and if there is the occurrence of that key inside another key to unset it… doesn’t seem to be working for me though. Am I missing something obvious?

Thank you in advance.

-H

  • 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-14T07:36:13+00:00Added an answer on June 14, 2026 at 7:36 am

    You’re mis-using strpos/stripos. They can return a perfectly valid 0 if the string you’re searching for happens to be at the START of the ‘haystack’ string, e.g. your Bob value. You need to explicitly test for this with

    if (stripos($k, $key) !== FALSE) {
       unset(...);
    }
    

    if strpos/stripos don’t find the needle, they return a boolean false, which under PHP’s normal weak comparison rules is equal/equivalent to 0. Using the strict comparison operators (===, !==), which compare type AND value, you’ll get the proper results.

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

Sidebar

Related Questions

Can this line of code be made more compact? db_tables_scanned = {} IO.readlines(resume_log_file).each {
How can this code compile? The code below in the operator int CAN access
Can this cast fail and when? long x=-1; long y = (long)(void*)x; assert(x==y); More
How can this MATLAB code be translated to Python? For example with random files:
How can this code be converted to COBOL? Result := GetSysColor(Color and $000000FF) The
why can this code not access 'borderWidth' (iphone code attached) This is based on
Can this code if (typeof foo != undefined && foo !== null) { }
Can this code throw an exception? public String[] GetPorts() { var sourceArray = Data;
How can this be done? This code isn't working: //////////INIT COLOR VBO FOR USE
can this code snippet be modified to reveal html coding? <?php $_option = $this->getOption()

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.