How to properly confront values from array and echo the result?

Viewed 61

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 !

3 Answers

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;
}


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
)

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";        
    }
}


?>
Related