Zhicken -3 years ago 106
PHP Question

# Finding the greatest, least and middle value

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
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download