Home > Backend Development > PHP Tutorial > 数组计算差值及项的小计,该如何处理

数组计算差值及项的小计,该如何处理

WBOY
Release: 2016-06-13 11:54:52
Original
916 people have browsed it

数组计算差值及项的小计

本帖最后由 lazygc520 于 2014-04-14 16:19:41 编辑 $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 (<br />  0 => array (<br />    0 => array (<br />      0 => '2014-04-11', 'time' => '2014-04-11',<br />      1 => 'BEA01-120N', 'type' => 'BEA01-120N',<br />      2 => 176, 'count' => 176,<br />    ),<br />    1 => array (<br />      0 => '2014-04-11', 'time' => '2014-04-11',<br />      1 => 'BEA21-110N', 'type' => 'BEA21-110N',<br />      2 => 1056, 'count' => 1056,<br />    ),<br />  ),<br />  1 => array (<br />    0 => array (<br />      0 => '2014-04-11', 'time' => '2014-04-11',<br />      1 => 'BEA01-120N', 'type' => 'BEA01-120N',<br />      2 => 192, 'count' => 192,<br />    ),<br />    1 => array (<br />      0 => '2014-04-11', 'time' => '2014-04-11',<br />      1 => 'BEA21-110N', 'type' => 'BEA21-110N',<br />      2 => 960, 'count' => 960,<br />    ),<br />  ),<br />);<br /><br />function foo($a, $b) {<br />  for($i=0; $i<count($a); $i++) {<br />    $res[] = array($a[$i]['type'], $a[$i]['count'] - $b[$i]['count']);<br />  }<br />  return $res;<br />}<br /><br />$t = call_user_func_array('foo', $s);<br />print_r($t);  
Copy after login
Array<br />(<br />    [0] => Array<br />        (<br />            [0] => BEA01-120N<br />            [1] => -16<br />        )<br /><br />    [1] => Array<br />        (<br />            [0] => BEA21-110N<br />            [1] => 96<br />        )<br /><br />)<br /><br />
Copy after login
小计你已经做了,我就偷懒了
------解决方案--------------------
$s = array (
  0 => array (
    0 => array (
      0 => '2014-04-11', 'time' => '2014-04-11',
      1 => 'BEA01-120N', 'type' => 'BEA01-120N',
      2 => 176, 'count' => 176,
    ),
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template