php 数组不能算加减吗,数组加减有关问题(2)

你之前不是问过这个问题了么?

如果你要对类似数组做很多纵向(column)计算的话,我建议你不如做个行列转换,思路就开阔多了

大不了计算完成后再一次行列转换换回来就是了

------解决方案--------------------

不是数据库,就是php的数组,把一维和二维的key对调

$a[行][列] 转换成 $a[列][行] 而已

这样就可以把 $a[某列] 作为一个一维数组,做加减,交并差,累计都很方便

我记得以前发过的

------解决方案--------------------

//数组a:

$a = array (

0 =>

array (

'cust_no' => '310F6 1VA5A',

'lotno' => '2X15',

'part_count' => '32',

),

1 =>

array (

'cust_no' => '310F6 1VA5A',

'lotno' => '2Z25',

'part_count' => '32',

),

);

//数组b:

$b = array (

0 =>

array (

'cust_no' => '310F6 1VA5A',

'total' => '48',

),

);

foreach($b as $source) {

$num = $source['total'];

foreach($a as $i=>$dest) {

if($num == 0) break;

if($dest['cust_no'] != $source['cust_no']) continue;

if($num >= $dest['part_count']) {

$num -= $dest['part_count'];

$res[] = $dest;

$a[$i]['part_count'] = 0;

}else {

$dest['part_count'] = $num;

$res[] = $dest;

$a[$i]['part_count'] -= $num;

$num = 0;

}

}

}

var_export($res);

var_export($a);array (

0 =>

array (

'cust_no' => '310F6 1VA5A',

'lotno' => '2X15',

'part_count' => '32',

),

1 =>

array (

'cust_no' => '310F6 1VA5A',

'lotno' => '2Z25',

'part_count' => 16,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值