如何乘以包含测量值的多维数组的值

时间:2016-11-17 10:24:27

标签: php arrays multidimensional-array

我得到了一个包含测量值的下一个多维数组,以英寸为单位,用于确定盒子体积的3个盒子

length

现在我的问题是如何将盒子的值多重以回显每个盒子的音量?所以结果将是这样的: “小盒子的音量为: 300 。”

3 个答案:

答案 0 :(得分:1)

您可以使用foreach并迭代框数组。但请注意,如果缺少订单或属性,您可能会收到意外结果。

foreach ($box as $aBox){
    $iVolume = ($aBox[1] * $aBox[2] * $aBox[3]);
    echo "The volume of ".$aBox[0]." is: ".$iVolume;
}

答案 1 :(得分:1)

foreach ($box as $key => $value) {
  // assuming that values exists as depicted in question.
  $volume = ($value[1] * $value[2] * $value[3]);
  echo "Volume of " . $value[0] . " is " . $volume;
}

答案 2 :(得分:-1)

每个框的卷数组

$box = array(
        array("Small Box", 12, 10, 2.5),
        array("Medium Box", 30, 20, 4),
        array("Large Box", 60, 40, 11.5)
    );
$dimArr = array_map("removeBoxName", $box);
$volumnArr = array_map("calculateVolume", $dimArr);
print_r($volumnArr);

// Common functions
function removeBoxName($arrBox) {
    array_shift($arrBox);
    return $arrBox;
}

function calculateVolume($arrBox) {
    return array_product($arrBox);
}