Home >Backend Development >PHP Tutorial >求帮忙一下子.

求帮忙一下子.

WBOY
WBOYOriginal
2016-06-13 12:04:31782browse

求帮忙一下......
下面这个表是从数据库拿出来的.应该还可以拿到更多.
id          a                    b
0           10                 2
1           30                 5
2           50                 4

要求从a里拿最高的数,然后用这个数加上b其他的数
例如:   50+2+5=57

如果只用一个array应该怎样做?
或者有更好的解法吗?
(那些数字好象全是string来的)
------解决方案--------------------
直接在数据库里面查出来就可以了

<br />select max(concat(a,'-', b)) as a,sum(b) as b  from test<br />

结果:
  a        b  
50-4    11
50-4+11=57
------解决方案--------------------
$a = array(10, 30, 50);<br />$b = array(2, 5, 4);<br />$max = max($a);<br />$sum = 0;<br />foreach($a as $i=>$v)<br />  if($v < $max) $sum += $b[$i];<br />echo $max + $sum;

------解决方案--------------------
<br />$arr = array(<br />	array(10, 2),<br />	array(30, 5),<br />	array(50, 4)<br />);<br /><br />$index = 0;<br />$max = 0;<br />for($i=0,$len=count($arr); $i<$len; $i++){<br />	if($arr[$i][0]>$max){<br />		$max = $arr[$i][0];<br />		$index = $i;<br />	}<br />}<br /><br />$total = $max;<br />for($i=0,$len=count($arr); $i<$len; $i++){<br />	if($i==$index){<br />		continue;<br />	}<br />	$total += $arr[$i][1];<br />}<br /><br />echo $total; // 57<br />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn