coding_n00b Posted April 24, 2015 Share Posted April 24, 2015 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); Link to comment https://forums.phpfreaks.com/topic/295842-getting-a-common-value-from-3-loops-help-on-understanding-solution/ Share on other sites More sharing options...
Barand Posted April 25, 2015 Share Posted April 25, 2015 Use array_intersect $array1 = [1, 5, 10, 20, 40, 80]; $array2 = [6, 7, 20, 80, 100]; $array3 = [3, 4, 15, 20, 30, 70, 80, 120]; $common = array_intersect($array1, array_intersect($array2,$array3)); echo "Common values are " . join(', ', $common); //--> 20, 80 Link to comment https://forums.phpfreaks.com/topic/295842-getting-a-common-value-from-3-loops-help-on-understanding-solution/#findComment-1509910 Share on other sites More sharing options...
Barand Posted April 25, 2015 Share Posted April 25, 2015 Definition Test data : that data for which the program works. Your solution loops through the values in the three arrays and counts the occurrence of each value. If the occurrence is >2 it assumes it is a value common common to the three arrays. If however an array contained repeating values that would also appear as "common" in the solution EG in $array1 = [1, 5, 5, 10, 20, 40, 80]; $array2 = [5, 6, 6, 6, 7, 20, 80, 100]; $array3 = [3, 4, 15, 20, 30, 70, 80, 120]; 5 and 6 would also be wrongly included in the $commonValues array Link to comment https://forums.phpfreaks.com/topic/295842-getting-a-common-value-from-3-loops-help-on-understanding-solution/#findComment-1509911 Share on other sites More sharing options...
coding_n00b Posted May 4, 2015 Author Share Posted May 4, 2015 Use array_intersect $array1 = [1, 5, 10, 20, 40, 80]; $array2 = [6, 7, 20, 80, 100]; $array3 = [3, 4, 15, 20, 30, 70, 80, 120]; $common = array_intersect($array1, array_intersect($array2,$array3)); echo "Common values are " . join(', ', $common); //--> 20, 80 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. Link to comment https://forums.phpfreaks.com/topic/295842-getting-a-common-value-from-3-loops-help-on-understanding-solution/#findComment-1510768 Share on other sites More sharing options...
jcbones Posted May 5, 2015 Share Posted May 5, 2015 Hope this answers the latest question: <?php //data used for testing: $array1 = [1, 5, 10, 20, 40, 80]; $array2 = [6, 7, 20, 80, 100]; $array3 = [3, 4, 15, 20, 30, 70, 80, 120]; //declare a variable called values, empty array: $values = []; //combine arrays, assign them as $ar. foreach ([$array1, $array2, $array3] as $ar) { <-- take the 3 arrays and assign it to $ar - I get this //loop through each $ar index, pulling just the $value; foreach ($ar as $value) { <-- How do you get from 3 arrays in $ar to just the values from the arrays? //if there is not an index in the $values array, that is the same as the $value of the current $ar index. 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? //then set the index, and the counter to 0, this just declares the index so that it doesn't throw a notice. $values[$value] = 0; } //then we increment that value, so we have a count of the occurences. If this is the first occ, then we increment from 0 to 1. //*Note* no need to assign a variable name to this, it serves no purpose. $a = $values[$value]++; <-- Why do you increment this here for what purpose? } } //declare an empty array in variable $commonValues; $commonValues = []; //loop through the values array we created in the loop above. Remember that we are holding the numbers in index, and the count in value. foreach ($values as $value => $count) { <-- Can you explain what this foreach is doing? Stepping through it? //if the count is greater than 2, if ($count > 2) { //add it to the commonValues array, auto incrementing the index. $commonValues[] = $value; } } //print the array $commonValues to the screen, Note, there is a mis-spelling here. print_r($common_values); Link to comment https://forums.phpfreaks.com/topic/295842-getting-a-common-value-from-3-loops-help-on-understanding-solution/#findComment-1510868 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.