7 Replies - 10655 Views - Last Post: 15 October 2011 - 09:10 AM Rate Topic: -----

#1 Ghoztrider   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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   User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • 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!
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3802
  • View blog
  • Posts: 13,800
  • 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.
Was This Post Helpful? 2
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7261
  • View blog
  • Posts: 15,141
  • 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

Was This Post Helpful? 1
  • +
  • -

#5 Jstall   User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • 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();



Was This Post Helpful? 2
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3802
  • View blog
  • Posts: 13,800
  • Joined: 08-August 08

Re: Multiply values in a Multidimesional array

Posted 15 October 2011 - 08:15 AM

:blush: Oops. I grabbed a very old snippet of mine.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12362
  • View blog
  • Posts: 45,476
  • 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. :)
Was This Post Helpful? 1
  • +
  • -

#8 Ghoztrider   User is offline

  • New D.I.C Head

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

Re: Multiply values in a Multidimesional array

Posted 15 October 2011 - 09:10 AM

Great stuff. Thanks everyone.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1