CODE REQ: PHP Create a multidimensional array that contains the measurements, in
ID: 3672738 • Letter: C
Question
CODE REQ: PHP
Create a multidimensional array that contains the measurements, in inches, for several boxes that a shipping company might use to determine the volume of a box.
Formatted Code: http://pastebin.com/qynY6wsW
Check the pastebin I wrote the code already I just need some help on a little part which i describe below unless you can do it a better way.
I cannot figure out how to multiply the integers values together for each box - small box, medium box, largebox.
My Code
<!DOCTYPE html>
<html>
<head>
<title>DTD and Box Array</title>
</head>
<body>
<?php
$boxArray = array(
array('Small Box' , '12' , '10', '2.5' ),
array('Medium Box' , '30' , '20', '4' ),
array('Large Box' , '60' , '40', '11.5' ),
);
echo '<table border="1">';
echo '<tr><th>Boxes</th><th>Length</th><th>Width</th><th>Depth</th></tr>';
foreach( $boxArray as $boxArray )
{
echo '<tr>';
foreach( $boxArray as $key )
{
echo '<td>'.$key.'</td>';
}
echo '</tr>';
}
echo '</table>';
// Length * width * depth - dont know how get the integers.
// This echo isnt grabbing the integers i want
echo $boxArray[1][0] , $boxArray[1][0] ,$boxArray[3];
// Multiply small box length * width * height
?>
</body>
</html>
Explanation / Answer
You are getting the wrong valuse because you are replacing the whole boxArray with a new array in foreach loop, that is in each iteration of the loop, the boxArray will be assigned the corresponding inner array, so at last, the last inner arrays value will be the only value saved in boxArray.
Please find the foolowing corrected code :
<!DOCTYPE html>
<html>
<head>
<title>DTD and Box Array</title>
</head>
<body>
<?php
$boxArray = array(
array('Small Box' , '12' , '10', '2.5' ),
array('Medium Box' , '30' , '20', '4' ),
array('Large Box' , '60' , '40', '11.5' ),
);
echo '<table border="1">';
echo '<tr><th>Boxes</th><th>Length</th><th>Width</th><th>Depth</th></tr>';
foreach( $boxArray as $boxArray1 )
{
echo '<tr>';
foreach( $boxArray1 as $key )
{
echo '<td>'.$key.'</td>';
}
echo '</tr>';
}
echo '</table>';
// Length * width * depth - dont know how get the integers.
// This echo isnt grabbing the integers i want
echo $boxArray[0][1] * $boxArray[0][2] * $boxArray[0][3];
// Multiply small box length * width * height
?>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.