2

I have a script with 10 sets of numeric values. Every set has six different numeric values from 1 to 99.

<?php 
$set1 = ['23', '11', '52', '33', '1', '4']; 
$set2 = ['66', '70', '55', '8', '22', '1'];
$set3 = ['38', '21', '52', '51', '53', '9'];
$set4 = ['14', '31', '54', '5', '73', '39'];
$set5 = ['10', '3', '22', '59', '73', '39'];
$set6 = ['22', '13', '4', '5', '73', '39'];
$set7 = ['40', '3', '22', '5', '13', '30'];
$set8 = ['88', '53', '4', '25', '71', '19'];
$set9 = ['10', '30', '49', '25', '73', '46'];
$set10 = ['10', '3', '4', '5', '73', '11'];

How do i tell the script to

  • confront every value from all the sets
  • echo the numbers that appear in the sets 1 time, 2 times and 3 times ?

I'm trying with array_diff but I can't figure out how to make it work (I'm very new in php)

thanks for your help and your knowledge !

Francesco
  • 95
  • 6
  • What is your expected result? Please provide an example – endeavour Jun 11 '21 at 12:59
  • Sure, I would like to echo this way : Numbers that appear 1 time : 3, 4 Numbers that appear 2 times : 32, 43, 25 Numbers that appear 3 times : 52, 21, 30 Please note that values i wrote are just for example, the script should check all the sets and print the results. Thank you :) – Francesco Jun 11 '21 at 13:04
  • _appear 1 time_ in all the sets? – endeavour Jun 11 '21 at 13:05
  • yes, one time in all the sets, two times in all the sets, three times in all the sets. – Francesco Jun 11 '21 at 13:06
  • Can we see your code? Basically "just" iterate over each set and ... count the numbers. Where are you having troubles? – brombeer Jun 11 '21 at 13:08
  • I was trying with : $result=array_diff_key($set1,$set2,$set3,$set4,$set5,$set6,$set7,$set8,$set9,$set10); but i think is not the correct direction since i don't need to see the difference in the sets, instead i should find matches. But i don't know how..! :( – Francesco Jun 11 '21 at 13:14

3 Answers3

2

You can merge these arrays into one with array_merge(). This makes it easy to get the counts with array_count_values().

One you have the counts you can use a array_filter() to get the results you want.

Here is the working example http://sandbox.onlinephpfunctions.com/code/95cbae41e146b380d5ba405cdf4bfea4e018f07a

<?php
$set1 = ['23', '11', '52', '33', '1', '4'];
$set2 = ['66', '70', '55', '8', '22', '1'];
$set3 = ['38', '21', '52', '51', '53', '9'];
$set4 = ['14', '31', '54', '5', '73', '39'];
$set5 = ['10', '3', '22', '59', '73', '39'];
$set6 = ['22', '13', '4', '5', '73', '39'];
$set7 = ['40', '3', '22', '5', '13', '30'];
$set8 = ['88', '53', '4', '25', '71', '19'];
$set9 = ['10', '30', '49', '25', '73', '46'];
$set10 = ['10', '3', '4', '5', '73', '11'];

$mergedArray = array_merge($set1, $set2, $set3, $set4, $set5, $set6, $set7, $set8, $set9, $set10);

$counts = array_count_values($mergedArray);

$requiredCount = 3; ## Change this value to whatever counts you need

$requiredResult = array_filter($counts, function ($value) use ($requiredCount){
    return $value == $requiredCount;
});

var_dump($requiredResult);

## Or you can echo the keys like this
echo implode(', ', array_keys($requiredResult));

You can wrap the codes in a function to get numbers that appear X times in the set. This is the link to the working example http://sandbox.onlinephpfunctions.com/code/af414606a5e881b11638076e89342183182b8b80

<?php
$set1 = ['23', '11', '52', '33', '1', '4'];
$set2 = ['66', '70', '55', '8', '22', '1'];
$set3 = ['38', '21', '52', '51', '53', '9'];
$set4 = ['14', '31', '54', '5', '73', '39'];
$set5 = ['10', '3', '22', '59', '73', '39'];
$set6 = ['22', '13', '4', '5', '73', '39'];
$set7 = ['40', '3', '22', '5', '13', '30'];
$set8 = ['88', '53', '4', '25', '71', '19'];
$set9 = ['10', '30', '49', '25', '73', '46'];
$set10 = ['10', '3', '4', '5', '73', '11'];

$mergedArray = array_merge($set1, $set2, $set3, $set4, $set5, $set6, $set7, $set8, $set9, $set10);

echo 'Values that appear 1 time: ' . implode(', ', array_keys(getRepeatedNumber($mergedArray, 1))) . '<br>';
echo 'Values that appear 2 times: ' . implode(', ', array_keys(getRepeatedNumber($mergedArray, 2))) . '<br>';
echo 'Values that appear 3 times: ' . implode(', ', array_keys(getRepeatedNumber($mergedArray, 3))) . '<br>';

function getRepeatedNumber($mergedArray, $requiredCount)
{
    $counts = array_count_values($mergedArray);
    $requiredResult = array_filter($counts, function ($value) use ($requiredCount) {
        return $value == $requiredCount;
    });
    return $requiredResult;
}


endeavour
  • 576
  • 4
  • 15
  • @zanderwar Can you please point out where. Here is the working example http://sandbox.onlinephpfunctions.com/code/95cbae41e146b380d5ba405cdf4bfea4e018f07a – endeavour Jun 11 '21 at 13:27
  • Maybe not, not sure, the OP is a bit ambiguous, looks like you're missing a lot of results though; the output is missing several that appear 1, 2 or 3 times. – zanderwar Jun 11 '21 at 13:36
  • Pretty code though, individuality shown in each answer here haha, check out the others where one could add a new set without needing to update anything else <3 – zanderwar Jun 11 '21 at 13:39
  • @zanderwar No it is not, array_count_values does the hard work. Check the docs please. Also to get a count the $requiredCount value has to be changed. (OP can wrap it in a function if required) – endeavour Jun 11 '21 at 13:40
  • 1
    Sorry, but the output is missing several values, I'm sure whoever is interviewing this person would not expect to have to change the `$requiredCount` to get the entire result. <3 – zanderwar Jun 11 '21 at 13:42
  • @zanderwar Thats subjective now. OP needs a way to count the number of times a value occurs in a specific set of arrays. It can easily be changed to tailor any count, not just 1, 2 or 3. But doing that is also a trivial task – endeavour Jun 11 '21 at 13:44
  • 1
    @zanderwar Added the function to accommodate all _results_ as per the _task_. Thanks for pointing out. – endeavour Jun 11 '21 at 13:55
  • Haha some call me pedantic – zanderwar Jun 11 '21 at 13:56
1

Probably the hardest way there but hope you get the job!

http://sandbox.onlinephpfunctions.com/code/b814925df9d280a3715100eb2210b2c2fd4f5af3

<?php
$set1 = ['23', '11', '52', '33', '1', '4']; 
$set2 = ['66', '70', '55', '8', '22', '1'];
$set3 = ['38', '21', '52', '51', '53', '9'];
$set4 = ['14', '31', '54', '5', '73', '39'];
$set5 = ['10', '3', '22', '59', '73', '39'];
$set6 = ['22', '13', '4', '5', '73', '39'];
$set7 = ['40', '3', '22', '5', '13', '30'];
$set8 = ['88', '53', '4', '25', '71', '19'];
$set9 = ['10', '30', '49', '25', '73', '46'];
$set10 = ['10', '3', '4', '5', '73', '11'];

$next = 1;
$var = "set{$next}";
$count = [];

while (isset($$var)) {
    $set = $$var;
    
    foreach ($set as $val) {
        if (!isset($count[$val])) {
            $count[$val] = 0;
        }
        
        $count[$val]++;
    }

    $next++;
    $var = "set{$next}";
}

$result = [
    1 => 0,
    2 => 0,
    3 => 0
];

foreach ($count as $value => $occurrences) {
    if ($occurrences === 0 || $occurrences > 3)  {
        continue;
    }
    
    $result[$occurrences]++;
}

print_r($result);
Array
(
    [1] => 20
    [2] => 7
    [3] => 3
)
zanderwar
  • 3,440
  • 3
  • 28
  • 46
  • Thank you, that is very complex..! But i can't understand the meaning of the result it provides. It doesn't seem to echo the numbers appeared one, two and three times in the sets !? – Francesco Jun 11 '21 at 13:25
  • Numbers that appear 1 time: 20, Numbers that appear 2 times: 7 etc. Based on your comments in the post, maybe I took that too sincerely lol – zanderwar Jun 11 '21 at 13:26
  • http://sandbox.onlinephpfunctions.com/code/95475b5ded58a0166461dee1fcbeb7621c5fc064 – zanderwar Jun 11 '21 at 13:33
  • That not exactly what i was looking for, but is interesting. Thanks :) – Francesco Jun 11 '21 at 14:06
1

Assuming there is a non-specific number of arrays to count, but they each follow the same naming convention, you can use variable variables :

<?php 
$set1 = ['23', '11', '52', '33', '1', '4']; 
$set2 = ['66', '70', '55', '8', '22', '1'];
$set3 = ['38', '21', '52', '51', '53', '9'];
$set4 = ['14', '31', '54', '5', '73', '39'];
$set5 = ['10', '3', '22', '59', '73', '39'];
$set6 = ['22', '13', '4', '5', '73', '39'];
$set7 = ['40', '3', '22', '5', '13', '30'];
$set8 = ['88', '53', '4', '25', '71', '19'];
$set9 = ['10', '30', '49', '25', '73', '46'];
$set10 = ['10', '3', '4', '5', '73', '11'];

$i = 1;
$data = array();

while(${"set" . $i} !== null) { 
    foreach(${"set" . $i} as $number) { 
        if(isset($data[$number])) { 
            $data[$number] += 1;
        } else { 
            $data[$number] = 1;
        }
    }
    $i++;
}
foreach($data as $number => $count) { 
    if($count < 4) { 
        echo "$number appears $count times in all of the arrays \n";        
    }
}


?>
Giles Bennett
  • 1,509
  • 1
  • 12
  • 15