dongzhucha3999 2016-01-24 21:30
浏览 23
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • dongzhang1987 2016-01-24 22:09
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)