-1

Absolutely doing my head in here over something that I'm sure is very simple...

I have 2 arrays.

  • $post_cats which are categories that any given post is in.
  • $ad_cats which is an array of categories in which ads are placed.

Basically, if a post has in its array of selected categories, a category that matches an item in the array of ad categories, then it must return the matching value/item.

$post_cats returns this

array(4) {    
  [0]=> array(1) { ["slug"]=> string(6) "energy" }    
  [1]=> array(1) { ["slug"]=> string(6) "global" }  
  [2]=> array(1) { ["slug"]=> string(8) "identify" }  
  [3]=> array(1) { ["slug"]=> string(5) "south" }  
}

and $ad_cats returns this

array(6) {   
  [0]=> array(1) { ["slug"]=> string(5) "north" }  
  [1]=> array(1) { ["slug"]=> string(5) "south" }  
  [2]=> array(1) { ["slug"]=> string(4) "east" }  
  [3]=> array(1) { ["slug"]=> string(4) "west" }  
  [4]=> array(1) { ["slug"]=> string(6) "global" }  
  [5]=> array(1) { ["slug"]=> string(8) "fallback" }  
}

The duplicated item there is "south", so in my mind the value of array_intersect($post_cats, $ad_cats); should be an array with a single item - "south", correct?

But its returning, what seems like, everything in either of the arrays... I can't for the life of me get it to work..

Using the above example, I need to return "south" to a variable.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Mark
  • 407
  • 7
  • 17

3 Answers3

0

So you are looking for items that are in both arrays? ...

What about something like this:

function find_duplicate($array1, $array2)
{
  $list = array();

  foreach($array1 as $value1)
  {
    foreach($array2 as $value2)
    {
     if($value1 == $value2) $list[] = $value1;
    }
  }

  return $list;
}
Buksy
  • 11,571
  • 9
  • 62
  • 69
  • 1
    This has more syntax errors than a script written by my cat jumping on the keyboard. – Shoe Mar 08 '13 at 21:24
  • 2
    Still wrong. You are treating those arrays as one dimensional arrays, they are bi-dimensional. – Shoe Mar 08 '13 at 21:30
  • 1
    @Jueecy: yes, I have to admit I haven't fully read the question, just quickly written the example ... but seems like it does work for Mark, if not, let me know I will update it ... – Buksy Mar 08 '13 at 21:34
0

The best way is to convert those arrays in arrays array_intersect can work with. Considering:

$a; // first array
$b; // second array

then you would go with:

$a1 = array();
foreach ($a as $v) $a1[] = $v['slug'];

$b1 = array();
foreach ($b as $v) $b1[] = $v['slug'];

$c = array_intersect($a1, $b1);

PHP functions usually work with more powerful algorithms than what you may think; therefore it's a good choice to let PHP functions handle this kind of things.

Shoe
  • 74,840
  • 36
  • 166
  • 272
0

This solution uses array_map to get at the values and takes the intersection of that

function mapper($a) 
{
    return $a['slug'];
}

$set1 = array_map('mapper', $post_cats);
$set2 = array_map('mapper', $ad_cats);
$result = array_intersect($set1, $set2);

PhpFiddle for testing.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198