0

I'm comparing the results of two exploded strings (results from a query), though when I use array_intersect to find the overlap of the arrays, I unfortunately only receive the overlap of those tags which are come first in each array...so for example if two arrays look like this:

Array1:

array(
  [0]=> tag_a
  [1]=> tag_b
)

Array2:

array(
  [0]=> tag_a
  [1]=> tag_b
)

Array_Intersect is only returning tag_a as a match. I expected the behavior of array_intersect to return tag_a as well as tab_b.

As you can see later in my code, I'm then using the matches (tags present in both arrays) to build the array contactarray. I'm able to build the array OK, it just doesn't contain the values I would have expected (ex: tag_b).

EDIT I've run several tests printing the contactarray and have applied various tag strings to those contacts and only the contacts who have have tag_a first (in the array) are being returned even though several other contacts have tag_a, though it's just not first in the array. Thoughts?

if ($frequency == 'Weekly')
{
  $data['query_tag'] = $this->db->get('tags');

  foreach ($data['query_tag']->result() as $row2)
  {
    $contact_tags = $row2->tags;
    $contact_tags_exploded = explode(",", $contact_tags);

    $rule_tags_exploded = explode(",", $rule_tags);
    $result = array_intersect($rule_tags_exploded, $contact_tags_exploded);

    if(isset($result) && count($result) != 0){
      $contactarray[] = $row2->contact_name;
    }
  }
}
Mischa
  • 42,876
  • 8
  • 99
  • 111
mhawk
  • 29
  • 6
  • You're only checking if the result of `array_intersect` is set and that it contains more than 0 items. Other than that you're doing nothing with it... I'm guessing `tag_b` is in there, you're just not using it. – Mischa Apr 09 '13 at 12:38
  • Sorry, reading my post again, I was not very clear....To provide more context, I've run several tests printing the contactarray and have applied various tag strings to those contacts and only the contacts who have have tag_a first (in the array) are being returned even though several other contacts have tag_a, though it's just not first in the array. – mhawk Apr 09 '13 at 12:49
  • @Mischa had you truly paid attention by reformatting his code, you would've noticed the `$` sign I accidentally deleted. – Funk Forty Niner Apr 09 '13 at 12:57
  • @Fred, that's an edit on [another question](http://stackoverflow.com/questions/15902170/php-html-dom-parse-all-links-recursively) that I have not touched. I am talking about the fact that you removed the indentation on **this question** which makes the question harder to read. – Mischa Apr 09 '13 at 13:03

3 Answers3

2

Try array_uintersect()

Here $arr1 is your 1st array and $arr2 is second array

$intersect = array_uintersect($arr1, $arr2, 'compareDeepValue');
print_r($intersect);

function compareDeepValue($val1, $val2)
{
   return strcmp($val1['value'], $val2['value']);
}

This should give you both the values

Engineer
  • 5,911
  • 4
  • 31
  • 58
  • Yea, I saw this in another thread, though when I try this I receive this error message: Message: array_uintersect() [function.array-uintersect]: Not a valid callback compareDeepValue – mhawk Apr 09 '13 at 12:51
  • Try defining the compareDeepValue function above the $intersect = array_uintersect($arr1, $arr2, 'compareDeepValue'); – Engineer Apr 09 '13 at 12:59
0

Not Sure where is the problem you are facing copy paste this code and you will see the two values properly.

$arr = array( 'tag_a','tab_b ');

$arr = array('tag_a','tab_b ');
print_r(array_intersect($arr, $arr));
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
  • Sorry, reading my post again, I was not very clear....To provide more context, I've run several tests printing the contactarray and have applied various tag strings to those contacts and **only** the contacts who have have tag_a first (in the array) are being returned even though several other contacts have tag_a, though it's just not first in the array. – mhawk Apr 09 '13 at 12:48
0

use master array for first argument and array to compare as second argument. I am not sure what problem you have.

RiderHood
  • 79
  • 1
  • 1
  • 7