dshgdhdfcas30210 2018-05-09 01:58
浏览 27
已采纳

如何划分两个数组值?

I have 2 arrays with the same dimension called

$array1 = ['10','20','30'];

$array2 = ['5','10','5'];

I want the result $array1/$array2

$result = ['2','2','6'];

Any idea??

  • 写回答

2条回答 默认 最新

  • duanduo0520 2018-05-09 02:08
    关注

    You could use array_map, but in the background it's just looping the values anyways.
    Because of that I genrally use a loop myself and I can controll what the output should be.

    $array1 = ['10','20','30'];
    $array2 = ['5','10','5'];
    
    foreach($array1 as $key => $val){
        $result[$key] = $val/$array2[$key];
    }
    
    var_dump($result);
    

    will give you the expected output.
    But you could also do something like this in a loop:

    $array1 = ['10','20','30'];
    $array2 = ['5','10','5'];
    
    foreach($array1 as $key => $val){
        $result[$val. "/" . $array2[$key]] = $val/$array2[$key];
    }
    
    var_dump($result);
    

    which will give you:

    array(3) {
          ["10/5"] => 2
          ["20/10"] => 2
          ["30/5"] => 6
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部