Kevin.a
Kevin.a

Reputation: 4306

Check if item exists in multiple arrays

I have a function which returns an array:

This is the fucntion:

{
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();
    $product_names=array();
    foreach($items as $item => $values) { 
        $_product = $values['data']->post; 
        $product_names[]=$_product->post_title; 
}     

    $allproductname=print_r($product_names, true);
    return $allproductname;
}

The output of this function:

Array(
[0] => Social media campagne    
[1] => Marketingcampagne
)

I also have 2 arrays named group1 and group2.

$group1=array("Social media campagne","Marketingcampagne");

$group2=array("SEO","Facebook marketing");

Now what i want to check is if both of the values of the function output belong in $group1 then i want it to print "both values belong in group1"

And if 1 value belongs in group1 and one value in group 2 then echo "1 value belongs in group1 and one value belongs in group2"

--edit

I forgot to mention that this output can change its not always this:

Array(
[0] => Social media campagne    
[1] => Marketingcampagne
)

it can also be 3 products for example

Upvotes: 2

Views: 1471

Answers (2)

Seb
Seb

Reputation: 1551

To apply the array_intersect() from @GordonM to your problem (it seems like there are only strings in the arrays).

You first get the number of values from your output, that are in group1

 $inGroup1 = array_intersect($ouput, $group1);
 $inGroup1Count = count($inGroup1);   // you can rewrite these two lines into one, 
                                      // if the actual matches are of no interest

If this is the same count as $output, than it's group1. If not, check how many are in group2. I guess you can code that yourself. And then check if inGroup1Count and inGroup2Count are both > 0. Than it's in both groups.

For logic reason you should also check if all are in group 2. Or is that impossible?

With checking both groups you can create an output like

 echo $inGroup1Count.' value(s) belongs in group1 and '.$inGroup2Count.' value(s) belongs in group2';

Upvotes: 1

GordonM
GordonM

Reputation: 31760

It looks like what you want to do relates to set theory. You have a set of things (the return from the function) and you want to see if all the elements in that array are in another array (You want to check if the return array is a strict subset of the array you're checking against).

You can use array_intersect () to do this. This function takes at least 2 arrays as its arguments, and it returns an array of elements that exist in all the arrays passed to it.

If you have an array specifying all the values you want to check against and another array that may or may not be a subset of that array then you can use array_intersect to get a list of all the elements in both arrays. If the number of elements in the output match the number of elements in the array that you want to check then the array must be a strict subset of the array you're checking against.

The following code demonstrates the basic principle:

$set = [1, 2, 3, 4, 5, 6, 7, 8]; // Data you want to test against

$subset = [2, 4, 6, 8]; // Strict subset of $set
$notSubset = [2, 4, 6, 10]; // Not a strict subset of $set because it contains 10

var_dump (count ($subset) === count (array_intersect ($set, $subset))); // true
var_dump (count ($notSubset) === count (array_intersect ($set, $notSubset))); // false

NOTE: array_intersect is only really suitable when the contents of the arrays being compared are all primitive types, as PHP will cast them all to string while comparing them. If you're comparing arrays of arrays or arrays of object then you're better off using array_uintersect () and specifying how the elements are compared yourself with a callback function.

Upvotes: 1

Related Questions