Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Array sorting: any number of the elements */
- $arr1 = [4 , 3, 4, 5, 3, 8];
- $arr2 = [6 , 4, 2, 5, 6, 6, 8, 9, 10];
- function SortAny($arr){
- $size = count($arr)-1;
- $temp;
- for ($i = 1; $i <= $size; $i++) {
- $temp=$arr[$i];
- for ($j = $i -1; $j >= 0 && $arr[$j] > $temp; $j--){
- $arr[$j+1] = $arr[$j];
- }
- $arr[$j+1] = $temp;
- }
- return $arr;
- }
- /* Arrays: finding common elements */
- function FindCommonElements($arr1, $arr2) {
- $arr1 =SortAny($arr1);
- $arr2 = SortAny($arr2);
- $arr1size = count($arr1)-1;
- $arr2size = count($arr2)-1;
- for ($i=0; $i<=$arr1size; $i++){
- for ($j=0; $j <= $arr2size && $arr1[$i] !== $arr1[$i+1]; $j++){
- if ($arr1[$i]!==$arr2[$j] && $arr2[$j] !== $arr2[$j+1]){
- var_dump($arr2[$i]);
- }
- }
- }
- }
- echo FindCommonElements($arr1, $arr2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement