ホームページ >バックエンド開発 >PHPチュートリアル >項の差と小計の配列計算
$s = array ( 0 => array ( 0 => array ( 0 => '2014-04-11', 'time' => '2014-04-11', 1 => 'B -120N', 'タイプ' => 'BEA01-120N', 2 => 176, '计数' => 176, ), 1 => 数组 ( 0 => '2014-04-11', '時間' => '2014-04-11', 'BEA21-110N', 2 => 1056, '计数' => ' ), ), 1 => array ( 0 => array ( 0 => '2014-04-11', 'time' => '2014-04-11', 1 => 'BEA01- 120N ', 'タイプ' => 'BEA01-120N', 2 => 192, 'カウント' => 192, ), 1 => 配列 ( 0 => '2014-04-11', '時間 ' => '2014-04-11', 1 => 'BEA21-110N', 2 => 960, '计数' => 960 , ), )
その型に応じた差分と、$s[0]と$s[1]の型項目の小計を求めます。 (小計部分は unset を使用して実装されています)
理想的な結果:
小計の結果: BEA01-120N 176 差 BEA01-120N 192 96 = 27 -16 =
BEA21-110N 1056 BEA21- 110N 960 6-960 = 96
小計: 176+320 = 16 52
$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);
Array( [0] => Array ( [0] => BEA01-120N [1] => -16 ) [1] => Array ( [0] => BEA21-110N [1] => 96 ))あなたはすでにトリックを完了しました、そして私は怠け者です
$s = array (
0 =>array (
0 =>array (
0 => '2014-04-11', 'time' => '2014-04-11',
1 = > 'BEA01-120N','タイプ' =>'BEA01-120N',
2 => 176, ),
1 =>
0 => '2014-04-11','時間' => '2014-04-11',
1 => 'BEA21-110N', 'タイプ' => 'BEA21-110N',
2 => 1056, 'カウント' => 1056,
),
),
1 => 配列 (
0 => '2014-04-11', 'time 「 => 」 2014-04-11'、
1 => 'BEA01-120N', 'type' => 'BEA01-120N',
2 => 192, 'count' => )、
1 => 配列 (
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);