I am working with user supplied dimensions on a variety of products. Users supplied the length, width and height, but to calculate shipping costs I have take the longest measurement and set it as length and add that to the girth, which is calculated by adding the two shorter measurements and multiplying them by 2.
$PackageSize = length + (width*2 + height*2)
I can find which value is the highest using:
$newlength=max($length, $width, $height);
I can't figure out how to then figure out which are the two remaining values so I can plug them into the right place in the equation.
To me, the easiest way to do this is to leverage PHP's awesome array functions.
// create an array from the three dimensions
$sizes = array( $length, $width, $height );
// sorts the values from smallest to largest
sort( $sizes );
// assigns the sorted values to the variables width, height, length
list( $width, $height, $length ) = $sizes;
// Now, $length is the longest dimension, $width is shortest, and $height is the middle value
Why don't you sort? For example (if the values are numerical)
$values = [$length, $width, $height];
rsort($values);
$PackageSize = $values[0] + ($values[1]*2 + $values[2]*2);
Put the values into an array and sort the array. Then you can access them in order of value
$dimens = [$length, $width, $height]
rsort($dimens)
$dimens[0] // Is largest
$dimens[1] // Is next
$dimens[2] // Is smallest
You can put them in an array and sort it.
$length = 10;
$width = 7;
$height = 9;
$array = [$length,$width,$height];
Sort($array);
Echo "largest: " . $array[2] ."
";
Echo "the other two " . $array[1] . " " . $array[0]