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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

=== will be the death of me. Many thanks. I'll check you correct once SO lets me.
That if has also an additional problem according to what you want to achieve. See stackoverflow.com/a/13383210/367456
1

Don't forget as-well as needing !== false, you need $k != $key so your strings don't match themselves.

1 Comment

funny, I literally just added that :)
0

You have two problems inside your code-example:

  1. You combine each key with each key, so not only others, but also itself. You would remove all entries so because "Bob" is a substring of "Bob", too.
  2. stripos returns false if not found which can be confused with 0 that stands for found at position 0, which is also equal but not identical to false.

You need to add an additional check to not remove the same key and then fix the check for the "not found" case (Demo):

$baseTags = array(
    'Bob' => 2,
    'Theresa' => 3,
    'The farm house' => 2,
    'Bob at the farm house' => 1,
);

foreach ($baseTags as $key => $count)
{
   foreach ($baseTags as $k => $c)
   {
      if ($k === $key)
      {
          continue;
      }

      if (false !== stripos($k, $key))
      {
            unset($baseTags[$key]);
      }
   }
}

print_r($baseTags);

Output:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.