Question

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;
    }
  }
}
Was it helpful?

Solution

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

OTHER TIPS

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));

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top