Jump to content

coding_n00b

New Members
  • Posts

    6
  • Joined

  • Last visited

Everything posted by coding_n00b

  1. Yes, thanks I am aware you can use that but for learning purposes I am doing it without built-in PHP functions. My main concern was not understanding the inner foreach loop and how it related to the if not isset portion of the code.
  2. 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);
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.