How to check vice-versa combinations in PHP using CSV?

I have a input in CSV as below

  1. if product A has product B and product B has product A this input has to fail.Here, error has to throw for two rows for same category.
  2. if product A has product B and productB has empty and no reverse combination available this input should pass for same category.

scenario1: This has to fail both rows

productInit category productLast
productA     cat1     productB
productB     cat2     productA

scenario2: this has import successfully

productInit category productLast
productA     cat1     productB
productB     cat2    
public function productExists($productInit,$productLast,$data,$category){
 foreach ($data as $item) {
            if ($item['productInit'] === $productLast && $item['category'] === $category && $item['productLast'] === $productInit){
                return true;
    }
}

but it is throwing only for the reverse product not for both rows and scenario 2 is failing.