这就是我想要做的。我希望我的代码返回以下内容:
"小方框的音量为:300"
但我似乎无法弄明白该怎么做:
<?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 />";
}
?>
答案 0 :(得分:0)
您循环遍历$value
,计算并显示$sum
倍(3)次。只需计算产品:
foreach ($box as $number => $boxes)
{
echo "The volume of the "."$number"." is: \t";
echo array_product($boxes);
echo "<br />";
}
如果您没有包含length
,width
和depth
的可预测数组,那么完全没有任何意义,但您也可以这样做:< / p>
echo $boxes['length'] * $boxes['width'] * $boxes['depth'];
答案 1 :(得分:0)
$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 $key => $boxes) {
$volume = array_product($boxes);
echo "The volume of the $key is: $volume\n";
}
答案 2 :(得分:0)
你可以将盒子的大小变成索引数组
$sum = 1;/*set variable sum to 1 so if it's * in another numerics it's not 0*/
$i = 0; /*set i condition to reset value of condition*/
foreach ($box as $number => $boxes) {
foreach ($boxes as $value)
{
if (is_numeric($value)){
$sum = $sum*$value; /*it's product length*width*depth */
}
/*cek the condition if count of boxes is limit lengt so define variable data["box type"]*/
if(($i+1) == count($boxes)){
$data[$number] = $sum; /*give value for $data["box type"]*/
}
$i++;
}
$sum = 1;/*reset sum to 1*/
$i =0;/*reset i to 0*/
}
这是print_r($ data);
的结果Array
(
[Small box] => 300
[Medium box] => 2400
[Large box] => 27600
)
您可以使用。
将变量访问到任何类型的框echo "sample : The volume of the Medium Box is ". $data["Medium box"]." : \t";
,结果就像
sample : The volume of the Medium Box is 2400
如果您正在寻找,请告诉我。