-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-arrays.php
70 lines (58 loc) · 1.66 KB
/
merge-arrays.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
$names = ['Tina', 'Dana', 'Mike', 'Amy', 'Adam', 'Roland'];
$compare = ['Tina', 'Dean', 'Mel', 'Amy', 'Michael'];
// fun tangent from the exercise review
function caseInsenesitiveSearch($array, $searchTerm)
{
foreach ($array as $index => $value) {
if (strtolower($searchTerm) == strtolower($value)) {
return $index;
}
}
return false;
}
// takes an array and a search term
// returns true or false depending on if the search term is in the array
function arrayContains($haystack, $needle)
{
$result = array_search($needle, $haystack);
return $result !== false;
}
// will take two arrays and return the number of items in common between
// the two
function compareArrays($arrayOne, $arrayTwo)
{
// keep a count of the common elements
$count = 0;
// loop through one array for every item
foreach ($arrayOne as $value) {
// if the item is in the second array, increment our count
if (arrayContains($arrayTwo, $value)) {
$count += 1;
}
}
// return the count
return $count;
}
function combineArrays($a,$b){
if(count($a)>=count($b)){
$largerArray=$a;
$shorterArray=$b;
}else{
$largerArray=$b;
$shorterArray=$a;
}
$newArray=[];
foreach ($largerArray as $key => $value) {
if (in_array($value, $shorterArray)){
array_push($newArray, $value);
}else{
array_push($newArray, $value);
if (isset($shorterArray[$key])) {
array_push($newArray, $shorterArray[$key]);
};
};
};
print_r($newArray);
};
combineArrays($names,$compare);