-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_arrays.php
34 lines (24 loc) · 969 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
34
<?php
$names = ['Tina', 'Dana', 'Mike', 'Amy', 'Adam'];
$compare = ['Tina', 'Den', 'Mel', 'Amy', 'Michael'];
function find($needle, $haystack) // seeing if $needle is in the $haystack, can be any variable.
{
if (array_search($needle, $haystack) === false) //if they are NOT, it is bool(false)
{
return FALSE; // then we return the "bool(false)"
} else {
return TRUE; // otherwise we have s true valur, that the needle IS in the haystack
}
}
//var_dump(find('Mikey', $names)); //mikey IS in $names
function array_common_count($array1, $array2)
{
$count = 0;
foreach ($array1 as $value) { //establishing that we are looking at all values in $array1
if (find($value, $array2)) { //are any of the $values of array on IN $array2
$count++; // incriment the count by one for ever $value in common.
}
} return $count;
}
var_dump(array_common_count($names, $compare)); //will render (int) for however many are in common.
?>