-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-arrays.php
33 lines (25 loc) · 938 Bytes
/
search-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
<?php
// Create a function that returns true or false if an array value is found. Search for Tina and Bob in $names. Create a function to compare 2 arrays that returns the number of values in common between the arrays.
$names = ["Tina", "Dana", "Mike", "Amy", "Adam"];
$compare = ["Tina", "Dean", "Mel", "Amy", "Michael"];
function inArray ($search, $names) {
if (array_search($search, $names) === false) {
return false;
} else {
return true;
}
}
var_dump(inArray("Amy", $names));
function compareArrays($array1, $array2) {
$numInCommon = 0;
foreach ($array1 as $item) {
if (array_search($item, $array2, true) !== false) {
$numInCommon++;
}
}
return $numInCommon;
}
var_dump(compareArrays($names, $compare));
$animals = ["cat", "dog", "mouse", "gorilla", "giraffe", "elephant", "kangaroo", "duck"];
$animals2 = ["mouse", "rat", "penguin", "gorilla", "dog", "koala"];
var_dump(compareArrays($animals, $animals2));