我正在使用用户提供的各种产品尺寸。用户提供了长度,宽度和高度,但是为了计算运输成本,我采用最长的测量并将其设置为长度并将其添加到周长,这是通过将两个较短的测量值相加并乘以2来计算的。
$PackageSize = length + (width*2 + height*2)
我可以使用以下方法找到最高的值:
$newlength=max($length, $width, $height);
我无法弄清楚如何找出剩下的两个值,以便我可以将它们插入等式中的正确位置。
答案 0 :(得分:2)
对我而言,最简单的方法是利用PHP的强大数组函数。
// 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
答案 1 :(得分:1)
你为什么不排序?例如(如果值是数字)
$values = [$length, $width, $height];
rsort($values);
$PackageSize = $values[0] + ($values[1]*2 + $values[2]*2);
答案 2 :(得分:1)
将值放入数组并对数组进行排序。然后,您可以按值
的顺序访问它们$dimens = [$length, $width, $height]
rsort($dimens)
$dimens[0] // Is largest
$dimens[1] // Is next
$dimens[2] // Is smallest
答案 3 :(得分:1)
您可以将它们放入数组中并对其进行排序。
$length = 10;
$width = 7;
$height = 9;
$array = [$length,$width,$height];
Sort($array);
Echo "largest: " . $array[2] ."\n";
Echo "the other two " . $array[1] . " " . $array[0]