dptdb84606 2017-03-30 15:00 采纳率: 0%
浏览 9
已采纳

数组乘以答案并返回它

This is what I am trying to do. I would like my code to return the following:

"The volume of the small box is: 300"

But I cannot seem to figure out what to do:

<?php
    $box = array(
       "Small box" => array("length" => 12, "width" => 10, "depth" => 2.5),
       "Medium box" => array("length" => 30, "width" => 20, "depth" => 4),
       "Large box" => array("length" => 60, "width" => 40, "depth" => 11.5)

    );
    foreach ($box as $number => $boxes)
    {
        echo "The volume of the "."$number"." is: \t";
        foreach ($boxes as $value)            
        {
            if (is_numeric($value))
            {
               $sum = array_product($boxes);
               echo "$sum";
            }
        }
        echo "<br />";
    }
    ?>
  • 写回答

3条回答 默认 最新

  • dongxinxin7809 2017-03-30 15:06
    关注

    You are looping through the $value, calculating and displaying the $sum multiple (3) times. Just calculate the product:

    foreach ($box as $number => $boxes)
    {
        echo "The volume of the "."$number"." is: \t";
        echo array_product($boxes);
    
        echo "<br />";
    }
    

    If you don't have a predictable array with length, width and depth then there is little point in doing this at all, but you could also do:

        echo $boxes['length'] * $boxes['width'] * $boxes['depth'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教