Jump to content

Getting a common value from 3 loops - help on understanding solution


coding_n00b
Go to solution Solved by Barand,

Recommended Posts

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
Share on other sites

  • Solution

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
Share on other sites

  • 2 weeks later...

 

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.

Edited by coding_n00b
Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.