Advertisement
AviEzerzer

Untitled

Dec 31st, 2020
1,265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php
  2.  
  3. $a1 = array("a" => "red", "a" => "red", "b" => "green", "c" => "blue", "d" => "yellow");
  4. $a2 = array("e" => "red", "e" => "red", "f" => "green", "g" => "blue");
  5.  
  6. $result = array("d" => "yellow");
  7.  
  8. // 0. if no dupes return
  9. // 1. take a1 and find duplicates
  10. $a1dupes = array("a" => "red");
  11. // 2. if a2 NOT include "red" return
  12. // else if "red"<2 return
  13. // result:
  14. $result = array("red");
  15.  
  16.  
  17. function findDupesInBothArrays($arr1, $arr2)
  18. {
  19.     $a1dupes = array();
  20.     for ($i = 0; $i < count($arr1); $i++) {
  21.         # code...
  22.        $valueToCheck = $arr1[$i];
  23.         for ($j = 0; $j < count($arr1); $j++) {
  24.             # code...
  25.            if ($i == $j) {
  26.                 return;
  27.             }
  28.             if ($valueToCheck == $arr1[$j]) {
  29.                 // $a1dupes[$key] = $valueToCheck;
  30.                 array_push($a1dupes, $valueToCheck);
  31.             }
  32.         }
  33.     }
  34.  
  35.     // $a1dupes = array("a" => "red");
  36.     for ($i = 0; $i < count($a1dupes); $i++) {
  37.         # code...
  38.        $valueToCheck = $a1dupes[$i];
  39.         for ($j = 0; $j < count($arr2); $j++) {
  40.             # code...
  41.            if ($valueToCheck == $arr2[$j]) {
  42.                 // $a1dupes[$key] = $valueToCheck;
  43.                 array_push($result, $valueToCheck);
  44.             }
  45.         }
  46.     }
  47.  
  48.     if (count($result)) {
  49.         return $result;
  50.     }
  51.  
  52.     return null;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement