Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP Find same data in an array?

is i want Finds data that is in an array.
where it takes data from $data to find data in $data_set
By taking out the same information, for example, get data 3, 4, 1, 5.
Let’s find the information contained in $data_set.
where the desired result is G, B

 $data = [3, 4, 1, 5];
 $data_set = [
   'R' => [3, 2, 1],
   'G' => [3, 4],
   'B' => [1, 5],
 ];

output = G,B

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

for the question
how to obtain this output = G,B

it’s only a simple search

<?php
$data = [3, 4, 1, 5];
 $data_set = [
   'R' => [3, 2, 1],
   'G' => [3, 4],
   'B' => [1, 5],
 ];

$res='';
foreach( $data_set as $name => $search){
    $s=count($data_set[$name]);
    $x=0;
    foreach($search as $vl){
        if(in_array($vl,$data))  $x++;
    }
    if($s==$x) $res.= ' '.$name.',';
}
echo substr($res,0,-1);
?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading