I need some help understanding the solution on how to get common values from 3 arrays.
Questions on the bits of the code I don't understand is in red.
Here is the code and solution:
$array1 = [1, 5, 10, 20, 40, 80]; $array2 = [6, 7, 20, 80, 100]; $array3 = [3, 4, 15, 20, 30, 70, 80, 120]; $values = []; foreach ([$array1, $array2, $array3] as $ar) { <-- take the 3 arrays and assign it to $ar - I get this foreach ($ar as $value) { <-- How do you get from 3 arrays in $ar to just the values from the arrays? if (!isset($values[$value])) { <-- If the values array passing in the array values variable $value is not set then it equals 0 / why is this? $values[$value] = 0; } $a = $values[$value]++; <-- Why do you increment this here for what purpose? } } $commonValues = []; foreach ($values as $value => $count) { <-- Can you explain what this foreach is doing? Stepping through it? if ($count > 2) { $commonValues[] = $value; } }
print_r($common_values);