0

Hello all I have a quick question that I'm hoping one of you can answer. I am working with multidimensional arrays and I am supposed to declare the width, length, and depth of a small, medium, and large box and then add statements that show the volume of each of the boxes. My problem comes in with the statement (I'm new to PHP). When running the script on WAMP I am returned a blank page so I am guessing that I did something wrong. Here's the code, thank you for any helpful answers.

<?php
$SmallBox =array("length" =>12, "width" =>10, "depth" =>2.5);
$MediumBox =array("length" =>30, "width" =>20, "depth" =>4);
$LargeBox =array("length" =>60, "width" =>40, "depth" =>11.5);
echo $SmallBox ["length"]["width"] ["depth"];
echo $MediumBox ["length"] ["width"] ["depth"];


?>

3 Answers3

1

The arrays you have defined are single-dimensional, associative arrays.

You should instead define the array of boxes in this method:

// I've separated them into lines for easier readability
$boxes = array(
    "small" => array("length" =>12, "width" =>10, "depth" =>2.5),
    "medium" => array("length" =>30, "width" =>20, "depth" =>4),
    "large" => array("length" =>60, "width" =>40, "depth" =>11.5)
);
// Show the structure and contents of the array
echo '<h2>Array Structure</h2>';
print_r($boxes);

// You will have to access these elements individually
// Small box:
echo '<h2>Small Box</h2>';
echo 'Length: '.$boxes['small']["length"].'<br>';
echo 'Width: '.$boxes['small']["width"].'<br>';
echo 'Depth: '.$boxes['small']["depth"].'<br>';
echo 'Volume: '.$boxes['small']["length"]*$boxes['small']["width"]*$boxes['small']["depth"].'<br>';

// Medium box:
echo '<h2>MediumBox</h2>';
echo 'Length: '.$boxes['medium']["length"].'<br>';
echo 'Width: '.$boxes['medium']["width"].'<br>';
echo 'Depth: '.$boxes['medium']["depth"].'<br>';
echo 'Volume: '.$boxes['medium']["length"]*$boxes['medium']["width"]*$boxes['medium']["depth"].'<br>';
shrmn
  • 989
  • 10
  • 19
  • When I use your method, I am returned with "Array ( [small] => Array ( [length] => 12 [width] => 10 [depth] => 2.5 ) [medium] => Array ( [length] => 30 [width] => 20 [depth] => 4 ) [large] => Array ( [length] => 60 [width] => 40 [depth] => 11.5 ) ) 12102.530204 " when I execute the script through WAMP. – user5374923 Nov 02 '15 at 02:27
  • @user5374923 Yes that is a correct, expected output. It shows your multi-dimensional associative array. The numbers below are the individual numbers that you have echo-ed out (echo does not append a new line at the end. You will need to concatenate the new line yourself, which in the case of HTML, is
    . echo $boxes['small']["length"]; would then need to be changed to echo $boxes['small']["length"].'
    '; I will update the code sample to display this.
    – shrmn Nov 02 '15 at 03:05
  • Ah, I see. Thank you very much for clearing that up! – user5374923 Nov 02 '15 at 04:56
  • @user5374923 Cheers! Mark my answer if you find it relevant :) – shrmn Nov 02 '15 at 09:00
0

use print_r($SmallBox), this suppose to show warning 'cause echo cannot accept arrays, turn on error reporting for php,

if you want it to be formatted:

    print "<pre>";
    print_r($SmallBox);
    print "</pre>";
    exit;   

to know about the datatype and length use var_dump($SmallBox)

to turn on error reporting see this post: How do I get PHP errors to display?

Community
  • 1
  • 1
mmr
  • 528
  • 11
  • 29
-1

While you've created the arrays correctly, your echo statements aren't possible this way.

Try:

echo $SmallBox["length"];
echo $SmallBox["width"];
echo $SmallBox["depth"];

And so on.

cteski
  • 489
  • 3
  • 15
  • 17