Array sum operation

Source: Internet
Author: User
foreach ($a as $k = + $v) {print_r ($newarr); echo $a [$k] [' 5 ']. '
';}


Array (    [0] = = Array        (            [Lotno] = 3206            [Count] = +        )    [1] = = Array        (            [ Lotno] = 3207            [Count] [    2] = = Array        (            [Lotno] = 3218            [count] = 180< c14/>)) 120Array (    [0] = = Array        (            [Lotno] = 3206            [Count] = [    1] = = Array        (            [Lotno] = 3207            [count]    [2] = = Array        (            [Lotno] = 3218            [Count] =        135


The count is added and if the result of the corresponding row is 120, the 3206*12 3207*63 is displayed 3218*45
If the result of the corresponding row is 135, the 3218*135 is displayed (135=180-45 (last count used 45), not enough)


Reply to discussion (solution)

Do not understand what you mean, 45 is written dead or calculated? Stick to your final result.

Figure it out. 45 = 120-12-63.

The results are similar to the following:

Items corresponding to Quantity

120 3206*12 3207*63 3208*45

135 3208*135 (135 = 180-45 (previous result set))

This means

$ar = Array (' Lotno ' + 3206, ' count ' = =), array (' lotno ' = = 3207, ' count ' = +), array (' Lotno ' =& Gt 3218, ' count ' = +), $s = '; $n = 0;foreach ($ar as $r) {  if ($n + $r [' count '] >) {    echo $s. $r [' Lotn O ']. '*' . (120-$n). " \ n ";    echo $r [' Lotno ']. '*' . ($n = $r [' Count ']-(120-$n)). " \ n ";    $s = ';  } else {    $s. = "$r [lotno]* $r [Count]";    $n + = $r [' count '];    if ($n = =) {      echo "$s \ n";      $s = ";      $n = 0;}}}  
3206*12 3207*63 3218*45
3218*135

$ar = Array (    ' lotno ' + 3206, ' count ' = +),    array (' lotno ' = = 3207, ' count ' = +),    ar Ray (' lotno ' = 3218, ' count ' = +), $a =array (   array (' 5 ' =>120),   Array (' 5 ' =>135)); foreach ($a as $k = $v) {  echo $v [' 5 ']. "  ";    if ($v [' 5 ']==120) {        $c =0;    foreach (Array_slice ($ar, 0,-1) as $VL) {      $c + = $VL [' count '];    echo $VL [' Lotno ']. " * ". $VL [' Count ']." ";} $end =end ($ar); Echo $end [' Lotno ']. " *" . ($sub = $v [' 5 ']-$c). '
';} else if ($v [' 5 ']==135) { echo $end [' Lotno ']. " *" . ($end [' count ']-$sub);}}
  • 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.