0

I have 2 data in the form of arrays

$x  = [ 0,1,1,0,1,0,0,0,0 ];
$y  = [ 0,0,1,0,1,0,0,0,0 ];

how to determine the similarity value = 1 if X = Y and similarity value = 0 if X ≠ Y

manual example :

  1. if 0 is equal to 0 then the result is 1
  2. if 1 is equal to 1 then the result is 1
  3. if 0 is equal to 1 then the result is 0
  4. if 1 is equal to 0 then the result is 0

how to write the program code in the form of an array above ?

please help me.

Afis Julianto
  • 15
  • 1
  • 1
  • 3

2 Answers2

0

With javascript you can write a function

function arrayDiff(x, y){
  if(x==y){
    return 1;
  }
  return 0;
}

Call the function like

var diff = arrayDiff(X[0], Y[0]);

The solution is based on your question as far I can understand

Atiqul Alam
  • 181
  • 4
0

if you want to get result by checking each value individually try:-

function arrayDiff($x, $y){
   return $x==$y ? 1 : 0;
}

$x  = [ 0,1,1,0,1,0,0,0,0 ];
$y  = [ 0,0,1,0,1,0,0,0,0 ];

$result=arrayDiff($x[0],$y[0]);
print_r($result);
//results : 1 in this case

or if you want the result "in the form of an array" as you have mentioned in your query try:-

function myfunction($v1,$v2)
{
    return $v1==$v2 ? 1 : 0;
}

$x  = [ 0,1,1,0,1,0,0,0,0 ];
$y  = [ 0,0,1,0,1,0,0,0,0 ];
print_r(array_map("myfunction",$x,$y));
//results : Array ( [0] => 1 [1] => 0 [2] => 1 [3] => 1 [4] => 1 [5] => 1 [6] => 1 [7] => 1 [8] => 1 )
Sandeep Modak
  • 832
  • 1
  • 5
  • 10