Please help. The table below is taken out of the database. you can get more.
Id a B
0 10 2
1 30 5
2 50 4
We need to get the highest number from a, and then use this number to add the other number of B.
Example: 50 + 2 + 5 = 57
What should I do if I only use one array?
Or is there a better solution?
(The numbers seem to be all strings)
Reply to discussion (solution)
You can find it directly in the database.
select max(concat(a,'-', b)) as a,sum(b) as b from test
Result:
A B
50-4 11
50-4 + 11 = 57
SQL directly
Select a, (select sum (B) from tbl_name where a <T. a) as B from tbl_nameT
A B
10
30 2
50 7
What if I use php?
My idea is to put all a values in an array.
Put all B in another Array.
Take the maximum value from a and add the other values in B.
Is there any better?
$a = array(10, 30, 50);$b = array(2, 5, 4);$max = max($a);$sum = 0;foreach($a as $i=>$v) if($v < $max) $sum += $b[$i];echo $max + $sum;
$arr = array(array(10, 2),array(30, 5),array(50, 4));$index = 0;$max = 0;for($i=0,$len=count($arr); $i<$len; $i++){if($arr[$i][0]>$max){$max = $arr[$i][0];$index = $i;}}$total = $max;for($i=0,$len=count($arr); $i<$len; $i++){if($i==$index){continue;}$total += $arr[$i][1];}echo $total; // 57
$a = array(10, 30, 50);$b = array(2, 5, 4);$max = max($a);$i = array_search($max, $a);unset($b[$i]);echo $max + array_sum($b);