特定の項目の配列合計問題

WBOY
リリース: 2016-06-23 14:20:28
オリジナル
928 人が閲覧しました

配列 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' => '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' => '32',    'c1_time' => '2013-08-10 15:00:00',    'packageno' => 'CVT121015002',  ),  2 =>   array (    'stock_no' => 'SU13080800340',    'adress' => 'B',    'arr_time' => '2013-08-14 09:00:00',    'c_type' => 'P32E',    'cust_no' => '310F61VA5B',    'mount_total' => '2',    'total' => '32',    'c1_time' => '2013-08-10 15:00:00',    'packageno' => 'CVT121226001',  ),)
ログイン後にコピー


配列内の項目 cust_no に対応する packageno の結果と、stock_no に対応する mount_total 項目と total 項目の合計の結果を取得するにはどうすればよいですか?
cust_no が 310F61VA5A であると仮定すると、対応するパッケージ番号は CVT121225001 です。
cust_no が 310F61VA5B の場合、対応するパッケージ番号は CVT121225002、CVT121226001 です。

stock_noがSU13080800340の場合、取得されるmount_total結果は3(1+2)、合計結果は48(16+32)となります。

この効果を実現するにはどうすればよいですか?


ディスカッションへの返信 (解決策)

	$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' => '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' => '32',     'c1_time' => '2013-08-10 15:00:00',     'packageno' => 'CVT121015002',   ),   2 =>    array (     'stock_no' => 'SU13080800340',     'adress' => 'B',     'arr_time' => '2013-08-14 09:00:00',     'c_type' => 'P32E',     'cust_no' => '310F61VA5B',     'mount_total' => '2',     'total' => '32',     'c1_time' => '2013-08-10 15:00:00',     'packageno' => 'CVT121226001',   ), );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);		//去重复	$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' => '32',    'c1_time' => '2013-08-10 15:00:00',    'packageno' => 'CVT121015002',  ),  2 =>   array (    'stock_no' => 'SU13080800340',    'adress' => 'B',    'arr_time' => '2013-08-14 09:00:00',    'c_type' => 'P32E',    'cust_no' => '310F61VA5B',    'mount_total' => '2',    'total' => '32',    '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);print_r($b);print_r($c);
ログイン後にコピー

配列
(
] [310f61va5a] =>指定された値を持つ配列要素の packageno 値が必要な結果になります。

2 番目の質問は、配列要素の各 Stock_no フィールドの値を決定し、指定された値を持つ配列要素の 2 つの必須フィールドを格納します。値を配列に入力し、ループが終了したら重複を削除して加算します

$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' => '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' => '32',    'c1_time' => '2013-08-10 15:00:00',    'packageno' => 'CVT121015002',  ),  2 =>   array (    'stock_no' => 'SU13080800340',    'adress' => 'B',    'arr_time' => '2013-08-14 09:00:00',    'c_type' => 'P32E',    'cust_no' => '310F61VA5B',    'mount_total' => '2',    'total' => '32',    'c1_time' => '2013-08-10 15:00:00',    'packageno' => 'CVT121226001',  ),);$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);print_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
)

)

第一项OK,但是mount_total和total的求和结果不对,我要的是stock_no为SU13080800340时,求得的mount_total结果为3(1+2),total结果为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);print_r($res);
ログイン後にコピー


我想要得到的stockno的求和结果是mount_total为3(2+1),total结果为48(16+32)。

楼主这种问题  应该算是比较基础的  我有点好奇为什么会纠结于
Array
        (
            [0] => 16
            [1] => 32
            [2] => 32
        )
这种数组的加和? 你的需求应该是去了重复求和....  一个PHP函数的事情...

你的数组中三个stock_no的值不是一样的吗,都为 SU13080800340 吧,难道我眼花了?

那是你的数据给错了
    'cust_no' => '310F61VA5A',
    'mount_total' => '1',
    'total' => '16',

    'cust_no' => '310F61VA5B',
    'mount_total' => '2',
    'total' => '32',

    'cust_no' => '310F61VA5B',
    'mount_total' => '2',
    'total' => '32',

那是你的数据给错了
    'cust_no' => '310F61VA5A',
    'mount_total' => '1',
    'total' => '16',

    'cust_no' => '310F61VA5B',
    'mount_total' => '2',
    'total' => '32',

    'cust_no' => '310F61VA5B',
    'mount_total' => '2',
    'total' => '32',
那我再想想。

你的数组中三个stock_no的值不是一样的吗,都为 SU13080800340 吧,难道我眼花了?
那怎么去重复求和呢?

データが間違っているためです
'cust_no' => '310F61VA5A',
'mount_total' => '16',
= > '310F61VA5B',
'mount_total' => '2'、
'total' => '310F61VA5B'、
'mount_total' = > 'total' => '32'、
合計を繰り返したい場合はどうすればよいでしょうか?


それはあなたのデータが間違っているからです
'cust_no' => '310F61VA5A',
'mount_total' => '16',

'cust_no' = > '310F61VA5B'、

'mount_total' => '2'、
'total' => '310F61VA5B'、
'mount_total' =>
'total' => '32',
合計を繰り返したい場合はどうすればよいでしょうか?

和を繰り返して1階を見上げると....

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!