比较两个数组的值如何具有相同的键和长度

I want to compare the values of two arrays that have the same key (The activity) and have the same length.

Arrays

$anna_array = array("soccer" => "10", "basketball" => "20", "atletics" => "40");
$john_array = array("soccer" => "15", "basketball" => "15", "atletics" => "45");

I want to have something like:

<?php
if ($anna_array['soccer'] > $john_array['soccer']){
    $points = "1";
} else {
    $points = "0";
}
?>

Then I will use $points in the results:

<?php
foreach ($anna_array as $x => $x_value) {
$speler1_prestaties = "<span class=\"white bold\">".str_pad($count1++, 2, "0", STR_PAD_LEFT)."</span> ".$x.": 
<span class=\"orange\">".$x_value." "(".$points .")></span><br />";     
echo $player1_info;
?>

Any help will be appreciated.

First, you can retrieve the common keys first applying array_intersect_key and then array_keys.

$common_sports = array_keys(array_intersect_key($anna_array, $john_array));

Then you can use array_fill_keys to fill an array with the same keys find above

$points_anna_array = $points_john_array = array_fill_keys($common_sports, 0);

The array here generated is:

 array(3) {
  ["soccer"]=>
  int(0)
  ["basketball"]=>
  int(0)
  ["atletics"]=>
  int(0)
}

Now you can compare the activity of anna and john

foreach ($common_sports as $common_sport) {
    if ($anna_array[$common_sport] > $john_array[$common_sport]) {
        $points_anna_array[$common_sport]++;
    } else if ($anna_array[$common_sport] < $john_array[$common_sport]) {
        $points_john_array[$common_sport]++;
    }
}

At this point $points_anna_array's value would be this:

array(3) {
  ["soccer"]=>
  int(0)
  ["basketball"]=>
  int(1)
  ["atletics"]=>
  int(0)
}

and $points_john_array value:

array(3) {
  ["soccer"]=>
  int(1)
  ["basketball"]=>
  int(0)
  ["atletics"]=>
  int(1)
}

So:

foreach ($common_sports as $common_sport) {
    echo sprintf(
       'Anna(%s) %d vs John(%s) %d'."
",
       $common_sport, $points_anna_array[$common_sport],
       $common_sport, $points_john_array[$common_sport]
    );
}

This will output:

Anna(soccer) 0 vs John(soccer) 1
Anna(basketball) 1 vs John(basketball) 0
Anna(atletics) 0 vs John(atletics) 1

Demo.

To get the total score, you can use array_sum.

echo "John's total score: ", array_sum($points_john_array);
echo "Anna's total score: ", array_sum($points_anna_array);

Just use a foreach and compare via keys:

foreach($anna as $key => $val){
    if(array_key_exists($key, $john)){
         if($val > $john[$key]){
             $points["john"] += 1;
         } else {
             $points["anna"] += 1;
         }
    }
}

I hope that I have understood your problem correctly.