# Multiply values in a Multidimesional array

Page 1 of 1

## 7 Replies - 10863 Views - Last Post: 15 October 2011 - 09:10 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=251396&amp;s=06104f2c0f4bf0e69b2b9c25706cf3de&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Ghoztrider

Reputation: 0
• Posts: 28
• Joined: 28-March 11

# Multiply values in a Multidimesional array

Posted 15 October 2011 - 05:52 AM

Hi

Is it possible to multiply values in a multidimensional array? I hope there is a way to accomplish this.I want to get the volume of the small box or any box for that matter. If so, could someone point me in the right direction. I was hoping the following code would work, but it doesn't. I'm still learning php so I'm sure I've mucked up other other stuff in this code. Thanks for any assistance offered.

```   <?php
//Creating Box Array
\$Small_box = array("lengt"=>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);

//Combining arrays
\$Allboxes = array("Small"=>\$Small, "Medium"=>\$Medium, "Large"=>\$Large);

//Smallbox volume
\$SmallVolume = (\$Small[length]*[width]*[depth]);
echo "<p>The volume of the smallbox is " . \$SmallVolume . "</p>\n";
?>

```

Is This A Good Question/Topic? 0

## Replies To: Multiply values in a Multidimesional array

### #2 jonesa01

Reputation: 25
• Posts: 125
• Joined: 18-February 07

## Re: Multiply values in a Multidimesional array

Posted 15 October 2011 - 06:31 AM

There is one simple solutoion to your problem, all you need is the array_product function in php this is how it works with your example:

```<?php
//Creating Box Array
\$Small_box = array("lengt"=>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);

//Combining arrays
\$Allboxes = array("Small"=>\$Small, "Medium"=>\$Medium, "Large"=>\$Large);

//Smallbox volume
echo "<p>The volume of the smallbox is " . array_product(\$Small_box) . "</p>\n";
?>

```

Hope this helps!

### #3 CTphpnwb

• D.I.C Lover

Reputation: 3810
• Posts: 13,833
• Joined: 08-August 08

## Re: Multiply values in a Multidimesional array

Posted 15 October 2011 - 07:49 AM

If you want to use individual array elements you'd do it this way:
```   <?php
//Creating 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);

//Combining arrays
\$Allboxes = array("Small"=>\$Small, "Medium"=>\$Medium, "Large"=>\$Large);

//Smallbox volume
\$SmallVolume = (\$Small['length']*\$Small['width']*\$Small['depth']);
echo "<p>The volume of the smallbox is " . \$SmallVolume . "</p>\n";
?>
```

Note the quotes around the array keys.

Of course, a box is an object so it's best represented by an object:
```<?php

class box {
var \$length;
var \$width;
var \$height;
var \$volume;

function box_volume() {
\$this->volume = \$this->length * \$this->width * \$this->height;
}
}

session_start();

if(isset(\$_SESSION['mybox'])) {
\$mybox = \$_SESSION['mybox']; // if you have a box, increase it's dimensions.
\$mybox->length++;
\$mybox->width++;
\$mybox->height++;

} else {
\$mybox = new box; // create new object and assign values
\$mybox->length = 20;
\$mybox->height = 40;
}

\$mybox->box_volume(); // calculate the volume and display it.
echo "My box volume is ".\$mybox->volume."<br>";

\$_SESSION['mybox'] = \$mybox; // Save the object to a session variable.

?>
```

This way you could have an array of objects. They're easier to manage than muti-dimensional arrays.

### #4 baavgai

• Dreaming Coder

Reputation: 7361
• Posts: 15,285
• Joined: 16-October 07

## Re: Multiply values in a Multidimesional array

Posted 15 October 2011 - 08:00 AM

You understand in one place you said \$Small_box, in another \$Small?

Functions are still your friends. Lack of quotes is not:
```\$Allboxes = 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)
);

function getVolume(\$box) {
return \$box['length']*\$box['width']*\$box['depth'];
}

function getVolumeForType(\$boxName) {
global \$Allboxes;
return getVolume(\$Allboxes[\$boxName]);
}

echo "<p>The volume of the smallbox is " . getVolumeForType('Small') . "</p>\n";

```

This post has been edited by baavgai: 15 October 2011 - 08:00 AM

### #5 Jstall

• Lurker

Reputation: 434
• Posts: 1,042
• Joined: 08-March 09

## Re: Multiply values in a Multidimesional array

Posted 15 October 2011 - 08:00 AM

If you are going the object route you should use access modifiers rather than the deprecated "var" when declaring properties. Also a proper constructor would be nice to initialize properties. Something like:
```class Box
{
private \$length;

private \$width;

private \$height;

public function __construct(\$length,\$width,\$height)
{
\$this->length = \$length;
\$this->width = \$width;
\$this->height = \$height;
}

public function box_volume()
{
return \$this->length * \$this->width * \$this->height;
}
}

\$my_box = new Box(10,20,40);

echo "The volume of the box is " . \$my_box->box_volume();

```

### #6 CTphpnwb

• D.I.C Lover

Reputation: 3810
• Posts: 13,833
• Joined: 08-August 08

## Re: Multiply values in a Multidimesional array

Posted 15 October 2011 - 08:15 AM

Oops. I grabbed a very old snippet of mine.

### #7 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12494
• Posts: 45,628
• Joined: 27-December 08

## Re: Multiply values in a Multidimesional array

Posted 15 October 2011 - 08:29 AM

More on OOPHP if you are interested.

### #8 Ghoztrider

Reputation: 0
• Posts: 28
• Joined: 28-March 11

## Re: Multiply values in a Multidimesional array

Posted 15 October 2011 - 09:10 AM

Great stuff. Thanks everyone.