The summation problem of an array for an item

Source: Internet
Author: User
Known quantity Group A:
array (0 = = Array (' stock_no ' = ' SU13080800340 ', ' adress ' = ' B '), ' Arr_time ' = ' 2013-08-14 09:00:00 ', ' c_type ' = ' p32e ', ' cust_no ' = ' 310f61va5a ', ' mount_total ' =   ' 1 ', ' total ' = ' + ', ' c1_time ' = ' 2013-08-10 15:00:00 ', ' packageno ' = ' CVT121015001 ', ' 1 ' =  Array (' stock_no ' = ' SU13080800340 ', ' adress ' = ' B ', ' arr_time ' = ' 2013-08-14 09:00:00 ', ' C_type ' = ' p32e ', ' cust_no ' = ' 310f61va5b ', ' mount_total ' = ' 2 ', ' total ' = ' + ', ' c1_time ' and ' 201 3-08-10 15:00:00 ', ' packageno ' = ' CVT121015002 ',), 2 = = Array (' stock_no ' = ' SU13080800340 ', ' Ad    Ress ' + ' B ', ' arr_time ' = ' 2013-08-14 09:00:00 ', ' c_type ' = ' p32e ', ' cust_no ' = ' 310f61va5b ', ' Mount_total ' = ' 2 ', ' total ' = ' + ', ' c1_time ' = ' 2013-08-10 15:00:00 ', ' Packageno ' and ' CVT1212260 ' ("),") 


How to obtain the results of the corresponding Packageno of the items in the array and the sum of the stock_no corresponding to the Mount_total and total items respectively? cust_no
Assuming that the cust_no is 310f61va5a, the corresponding Packageno is CVT121225001.
When the Cust_no is 310f61va5b, the corresponding Packageno is cvt121225002,cvt121226001.

When the Stock_no is SU13080800340, the mount_total result is 3 (1+2) and the total result is 48 (16+32).

How to achieve this effect?


Reply to discussion (solution)

$a = Array (0 = = Array (' stock_no ' = ' SU13080800340 ', ' adress ' = ' B ', ' arr_time ' = ' + ' 2013 -08-14 09:00:00 ', ' c_type ' = ' p32e ', ' cust_no ' = ' 310f61va5a ', ' mount_total ' = ' 1 ', ' Total ' =& Gt ' + ', ' c1_time ' = ' 2013-08-10 15:00:00 ', ' packageno ' = ' CVT121015001 ', ', ' 1 = = Array (' STOC K_no ' + ' SU13080800340 ', ' adress ' + ' B ', ' arr_time ' = ' 2013-08-14 ' 09:00:00 ', ' c_type ' and ' p32e ' , ' cust_no ' = ' 310f61va5b ', ' mount_total ' and ' 2 ', ' total ' = ' + ', ' c1_time ' + ' 2013-08-10 1 5:00:00 ', ' packageno ' = ' CVT121015002 ',), 2 = = Array (' stock_no ' = ' SU13080800340 ', ' adres     s ' = ' B ', ' arr_time ' = ' 2013-08-14 09:00:00 ', ' c_type ' = ' p32e ', ' cust_no ' = ' 310f61va5b ', ' Mount_total ' = ' 2 ', ' total ' = ' + ', ' c1_time ' = ' 2013-08-10 15:00:00 ', ' Packageno ' and ' CVT121 ' 226001 ',),); function Foo1 ($cust _no, $a =array ()) {foreach ($a as $each) {if ($each [' cust_no '] = = $cust _no) {$res [] = $each [' Packageno '];}} if ($res) {return implode (",", $res);} Else{return false;}} function Foo2 ($stock _no, $a =array ()) {foreach ($a as $each) {if ($each [' stock_no '] = = $stock _no) {$mount _total[] = $each [' Mount_total ']; $total [] = $each [' Total '];}} $mount _total = Array_unique ($mount _total);//Repeat $total = Array_unique ($total); $res [' mount_total '] = Array_sum ($mount _ Total), $res [' total '] = Array_sum ($total); return $res;} Var_export (Foo1 ("310f61va5b", $a)), Var_export (Foo2 ("SU13080800340", $a));


' cvt121015002,cvt121226001 '
Array (' mount_total ' = 3, ' total ' = 48,)

$ar =array (0 = = Array (' stock_no ' = ' SU13080800340 ', ' adress ' = ' B ', ' arr_time ' = ' 2013-08-14    09:00:00 ', ' c_type ' = ' p32e ', ' cust_no ' = ' 310f61va5a ', ' mount_total ' = ' 1 ', ' total ' = ' 16 ', ' C1_time ' = ' 2013-08-10 15:00:00 ', ' packageno ' = ' CVT121015001 ', ', ' 1 = = Array (' stock_no ' = = ' SU13080800340 ', ' adress ' = ' B ', ' arr_time ' = ' 2013-08-14 09:00:00 ', ' c_type ' = ' p32e ', ' cust_no ' = ' 310f61va5b ', ' mount_total ' = ' 2 ', ' total ' = ' + ', ' c1_time ' = ' 2013-08-10 15:00:00 ', ' Packa Geno ' = ' CVT121015002 ',), 2 = = Array (' stock_no ' = ' SU13080800340 ', ' adress ' = ' B ', ' Arr_tim    E ' = ' 2013-08-14 09:00:00 ', ' c_type ' = ' p32e ', ' cust_no ' = ' 310f61va5b ', ' mount_total ' = ' 2 ', ' Total ' = ' + ', ' c1_time ' = ' 2013-08-10 15:00:00 ', ' packageno ' = ' CVT121226001 ', '); foreach ($ar as $v) {$a [$v [' Cust_no ']][]= $v [' Packageno ']; $b [$v [' Stock_no ']][]= $v [' mount_total ']; $c [$v [' Stock_no ']][]= $v [' Total '];} Print_r ($a);p Rint_r ($b);p rint_r ($c);

Array
(
[310F61VA5A] = = Array
(
[0] = CVT121015001
)

[310f61va5b] = = Array
(
[0] = CVT121015002
[1] = CVT121226001
)

)
Array
(
[SU13080800340] = = Array
(
[0] = 1
[1] = 2
[2] = 2
)

)
Array
(
[SU13080800340] = = Array
(
[0] = 16
[1] = 32
[2] = 32
)

)

The first problem loop array is to judge the value of each cust_no field as the value of the Packageno of the array element of the given value and add up the result you want.

The second problem loop array determines the value of each of the Stock_no fields as the value of the array element of the given value is stored in an array of the values of the two required fields, respectively, after the loop is removed and then added

$a = Array (0 = = Array (' stock_no ' = ' SU13080800340 ', ' adress ' = ' B ', ' arr_time ' = ' = ' 2013-08-1     4 09:00:00 ', ' c_type ' = ' p32e ', ' cust_no ' = ' 310f61va5a ', ' mount_total ' = ' 1 ', ' total ' = ' 16 ',  ' C1_time ' = ' 2013-08-10 15:00:00 ', ' packageno ' = ' CVT121015001 ', ', ' 1 = = Array (' stock_no ' = =  ' SU13080800340 ', ' adress ' = ' B ', ' arr_time ' = ' 2013-08-14 09:00:00 ', ' c_type ' = ' p32e ', ' cust_no ' = ' 310f61va5b ', ' mount_total ' = ' 2 ', ' total ' = ' + ', ' c1_time ' = ' 2013-08-10 15:00:00 ', ' pack Ageno ' = ' CVT121015002 ',), 2 = = Array (' stock_no ' = ' SU13080800340 ', ' adress ' = ' B ', ' Arr_ti    Me ' = ' 2013-08-14 09:00:00 ', ' c_type ' = ' p32e ', ' cust_no ' = ' 310f61va5b ', ' mount_total ' = ' 2 ', ' Total ' = ' + ', ' c1_time ' = ' 2013-08-10 15:00:00 ', ' packageno ' = ' CVT121226001 ', '), $res = Array (); fo Reach ($a as $item) {if(! isset ($res [$item [' Stock_no ']][$item [' Cust_no ']])  {$res [$item [' Stock_no ']][$item [' cust_no ']] = $item;    }else {$res [$item [' Stock_no ']][$item [' cust_no ']][' mount_total '] + = $item [' Mount_total '];    $res [$item [' Stock_no ']][$item [' cust_no ']][' total '] + = $item [' Total '];  $res [$item [' Stock_no ']][$item [' cust_no ']][' Packageno ']. = ', '. $item [' Packageno ']; }}foreach ($res as & $item) $item = Array_values ($item);p rint_r ($res);
Array
(
[SU13080800340] = = Array
(
[0] = = Array
(
[Stock_no] = SU13080800340
[Adress] = B
[Arr_time] = 2013-08-14 09:00:00
[C_type] = p32e
[Cust_no] = 310f61va5a
[Mount_total] = 1
[Total] = 16
[C1_time] = 2013-08-10 15:00:00
[Packageno] = CVT121015001
)

[1] = = Array
(
[Stock_no] = SU13080800340
[Adress] = B
[Arr_time] = 2013-08-14 09:00:00
[C_type] = p32e
[Cust_no] = 310f61va5b
[Mount_total] = 4
[Total] = 64
[C1_time] = 2013-08-10 15:00:00
[Packageno] = cvt121015002,cvt121226001
)

)

)

[code=php]
Array
(
[310F61VA5A] = = Array
(
[0] = CVT121015001
)

[310f61va5b] = = Array
(
[0] = CVT121015002
[1] = CVT121226001
)

)
Array
(
[SU13080800340] = = Array
(
[0] = 1
[1] = 2
[2] = 2
)

)
Array
(
[SU13080800340] = = Array
(
[0] = 16
[1] = 32
[2] = 32
)

)

The first item OK, but the sum of mount_total and total is not correct, I want to stock_no for SU13080800340, the mount_total result is 3 (1+2), total result is 48 (16+32).

$res = Array (), foreach ($a as $item) {  if (isset ($res [$item [' Stock_no ']][$item [' Cust_no ']]) {$res [$item [    ' Stock_no ']][$item [' cust_no ']] = $item;  } else {    $res [$item [' Stock_no ']][$item [' cust_no ']][' mount_total '] + = $item [' mount_total '];    $res [$item [' Stock_no ']][$item [' cust_no ']][' total '] + = $item [' total '];    $res [$item [' Stock_no ']][$item [' cust_no ']][' Packageno ']. = ', '. $item [' Packageno '];  }} foreach ($res as & $item) $item = Array_values ($item);p rint_r ($res);


The sum of the stockno I want to get is Mount_total 3 (2+1) and the total result is 48 (16+32).

Landlord This problem should be a comparative basis I'm a little curious about why it's tangled in
Array
(
[0] = 16
[1] = 32
[2] = 32
)
The addition of this array? Your needs should be to repeat the sum .... A PHP function thing ...

Is the value of three stock_no in your array not the same, all for SU13080800340, am I dazzled?

That's your data, it's wrong.
' Cust_no ' = ' 310f61va5a ',
' Mount_total ' = ' 1 ',
' Total ' = ' 16 ',

' Cust_no ' = ' 310f61va5b ',
' Mount_total ' = ' 2 ',
' Total ' = ' 32 ',

' Cust_no ' = ' 310f61va5b ',
' Mount_total ' = ' 2 ',
' Total ' = ' 32 ',

That's your data, it's wrong.
' Cust_no ' = ' 310f61va5a ',
' Mount_total ' = ' 1 ',
' Total ' = ' 16 ',

' Cust_no ' = ' 310f61va5b ',
' Mount_total ' = ' 2 ',
' Total ' = ' 32 ',

' Cust_no ' = ' 310f61va5b ',
' Mount_total ' = ' 2 ',
' Total ' = ' 32 ',
Then I'll think about it.

Is the value of three stock_no in your array not the same, all for SU13080800340, am I dazzled?
How to repeat the sum?

That's your data, it's wrong.
' Cust_no ' = ' 310f61va5a ',
' Mount_total ' = ' 1 ',
' Total ' = ' 16 ',

' Cust_no ' = ' 310f61va5b ',
' Mount_total ' = ' 2 ',
' Total ' = ' 32 ',

' Cust_no ' = ' 310f61va5b ',
' Mount_total ' = ' 2 ',
' Total ' = ' 32 ',
What if it is to repeat the sum?


That's your data, it's wrong.
' Cust_no ' = ' 310f61va5a ',
' Mount_total ' = ' 1 ',
' Total ' = ' 16 ',

' Cust_no ' = ' 310f61va5b ',
' Mount_total ' = ' 2 ',
' Total ' = ' 32 ',

' Cust_no ' = ' 310f61va5b ',
' Mount_total ' = ' 2 ',
' Total ' = ' 32 ',
What if it is to repeat the sum?

To repeat the sum of the head to see the first floor ....

  • Related Article

    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.