数组计算差值及项的小计

WBOY
Freigeben: 2016-06-23 13:59:01
Original
1108 Leute haben es durchsucht

$s = array ( 0 => array ( 0 => array ( 0 => '2014-04-11', 'time' => '2014-04-11', 1 => 'BEA01-120N', 'type' => 'BEA01-120N', 2 => 176, 'count' => 176, ), 1 => array ( 0 => '2014-04-11', 'time' => '2014-04-11', 1 => 'BEA21-110N', 'type' => 'BEA21-110N', 2 => 1056, 'count' => 1056, ), ), 1 => array ( 0 => array ( 0 => '2014-04-11', 'time' => '2014-04-11', 1 => 'BEA01-120N', 'type' => 'BEA01-120N', 2 => 192, 'count' => 192, ), 1 => array ( 0 => '2014-04-11', 'time' => '2014-04-11', 1 => 'BEA21-110N', 'type' => 'BEA21-110N', 2 => 960, 'count' => 960, ), ), ) 

求其type对应的差值和$s[0]及$s[1]的type项的小计。(小计部分已实现,用了unset)


理想得到的结果:

小计的结果: BEA01-120N 176           BEA01-120N 192                  差值:176-192 = -16
                         BEA21-110N  1056        BEA21-110N  960                             1056-960 = 96
                        小计:176+1056=1232            192+960 = 1152


回复讨论(解决方案)

$s = array (  0 => array (    0 => array (      0 => '2014-04-11', 'time' => '2014-04-11',      1 => 'BEA01-120N', 'type' => 'BEA01-120N',      2 => 176, 'count' => 176,    ),    1 => array (      0 => '2014-04-11', 'time' => '2014-04-11',      1 => 'BEA21-110N', 'type' => 'BEA21-110N',      2 => 1056, 'count' => 1056,    ),  ),  1 => array (    0 => array (      0 => '2014-04-11', 'time' => '2014-04-11',      1 => 'BEA01-120N', 'type' => 'BEA01-120N',      2 => 192, 'count' => 192,    ),    1 => array (      0 => '2014-04-11', 'time' => '2014-04-11',      1 => 'BEA21-110N', 'type' => 'BEA21-110N',      2 => 960, 'count' => 960,    ),  ),);function foo($a, $b) {  for($i=0; $i<count($a); $i++) {    $res[] = array($a[$i]['type'], $a[$i]['count'] - $b[$i]['count']);  }  return $res;}$t = call_user_func_array('foo', $s);print_r($t);  
Nach dem Login kopieren
Array(    [0] => Array        (            [0] => BEA01-120N            [1] => -16        )    [1] => Array        (            [0] => BEA21-110N            [1] => 96        ))
Nach dem Login kopieren
小计你已经做了,我就偷懒了

$s = array (
  0 => array (
    0 => array (
      0 => '2014-04-11', 'time' => '2014-04-11',
      1 => 'BEA01-120N', 'type' => 'BEA01-120N',
      2 => 176, 'count' => 176,
    ),
    1 => array (
      0 => '2014-04-11', 'time' => '2014-04-11',
      1 => 'BEA21-110N', 'type' => 'BEA21-110N',
      2 => 1056, 'count' => 1056,
    ),
  ),
  1 => array (
    0 => array (
      0 => '2014-04-11', 'time' => '2014-04-11',
      1 => 'BEA01-120N', 'type' => 'BEA01-120N',
      2 => 192, 'count' => 192,
    ),
    1 => array (
      0 => '2014-04-11', 'time' => '2014-04-11',
      1 => 'BEA21-110N', 'type' => 'BEA21-110N',
      2 => 960, 'count' => 960,
    ),
  ),
);
function my($a,$b){
    foreach($a as $k=>$v){
        echo $a[$k]['count']-$b[$k]['count']."
";
    }
}
call_user_func_array('my',$s);

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!