Home > Enterprise >  PHP Find same data in an array?
PHP Find same data in an array?

Time:07-02

I want to find 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

CodePudding user response:

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);
?>
  • Related