Advertisement
VeraKH

Untitled

Dec 3rd, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.81 KB | None | 0 0
  1. /* Array sorting: any number of the elements */
  2.  
  3.  
  4. $arr1 = [4 , 3, 4, 5, 3, 8];
  5. $arr2 = [6 , 4, 2, 5, 6, 6, 8, 9, 10];
  6.  
  7.  
  8. function SortAny($arr){
  9.  
  10. $size = count($arr)-1;
  11.  
  12. $temp;
  13.  
  14. for ($i = 1; $i <= $size; $i++) {
  15.     $temp=$arr[$i];
  16.     for ($j = $i -1; $j >= 0 && $arr[$j] > $temp; $j--){
  17.             $arr[$j+1]  = $arr[$j];
  18.         }
  19.         $arr[$j+1] = $temp;
  20. }
  21.     return $arr;
  22. }
  23.  
  24. /* Arrays: finding common elements */
  25.  
  26. function FindCommonElements($arr1, $arr2) {
  27.  
  28. $arr1 =SortAny($arr1);
  29. $arr2 = SortAny($arr2);
  30.  
  31. $arr1size = count($arr1)-1;
  32. $arr2size = count($arr2)-1;
  33.  
  34. for ($i=0; $i<=$arr1size; $i++){
  35.     for ($j=0; $j <= $arr2size && $arr1[$i] !== $arr1[$i+1]; $j++){
  36.         if ($arr1[$i]!==$arr2[$j] && $arr2[$j] !== $arr2[$j+1]){
  37.             var_dump($arr2[$i]);
  38.         }
  39.     }
  40. }
  41. }
  42.  echo FindCommonElements($arr1, $arr2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement