dshgdhdfcas30210 2018-05-09 09: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 10: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条)

报告相同问题?

悬赏问题

  • ¥15 jupyter notebook如何添加libGL.so.1库
  • ¥20 easyPoi能否实现下拉多选或者复选框
  • ¥15 网桥在转发帧时,会变帧的源地址和目的地址吗?
  • ¥15 用Multisim设计汽车尾灯控制电路
  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?