Please help-php Tutorial

Source: Internet
Author: User
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); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.