当前位置:Gxlcms > PHP教程 > 将变量数组重置

将变量数组重置

时间:2021-07-01 10:21:17 帮助过:24人阅读

foreach($arr as k1=>$v1){			$sql2 = "select cust_no,total=SUM(mount)*snp from ".$t." where po_num = '".$stockno."' and cust_no = '".$v1['cust_no']."' group by cust_no,snp";				$result2 = mssql_query($sql2);			$b = array();			while($row2 = mssql_fetch_array($result2))			{				$b[] = $row2;			}									foreach($b as $var)			{				$num = $var['total'];				foreach($a as $i=>$dest)				{					if($num == 0)						break;					if($dest['cust_no'] != $var['cust_no'])						continue;					if($num >= $dest['part_count'])					{						$num -= $dest['part_count'];						$ress[] = $dest;						$a[$i]['part_count'] = 0;					}					else					{						$dest['part_count'] = $num;						$ress[] = $dest;						$a[$i]['part_count'] -= $num;						$num = 0;					}				}			}			print_r($ress);}


如何把变量数组$ress重置?用了unset()还是没有效果。


回复讨论(解决方案)

没有看到 unse 在哪里
请说明需求

没有看到 unse 在哪里
请说明需求




对应的sql条件及数组$b的值:
col1:select cust_no,total=SUM(mount)*snp from dfl_stock_detail where po_num = 'SU14031300627' and cust_no = '237033AW0A' group by cust_no,snpArray ( [0] => Array ( [0] => 237033AW0A [cust_no] => 237033AW0A [1] => 375 [total] => 375 ) )col2: select cust_no,total=SUM(mount)*snp from dfl_stock_detail where po_num = 'SU14031300627' and cust_no = '237033RC3A' group by cust_no,snpArray ( [0] => Array ( [0] => 237033RC3A [cust_no] => 237033RC3A [1] => 75 [total] => 75 ) )


按照$b值的不同求对应的$ress的值。

但是现在$ress求出来,col2还是col1的结果。

unset 应该是可以用的 你看哈程序是不是跑到那个unset了 ,在那个unset处输出点东西看看,有啥子反映

没有看到 unse 在哪里
请说明需求


//已知数组a:array (  0 =>   array (    0 => '12653627',    'cust_no' => '12653627',    1 => '4069',    'lotno' => '4069',    2 => 19,    'part_count' => 19,  ),  1 =>   array (    0 => '12653627',    'cust_no' => '12653627',    1 => '4071',    'lotno' => '4071',    2 => 146,    'part_count' => 146,  ),  2 =>   array (    0 => '12654172',    'cust_no' => '12654172',    1 => '4071',    'lotno' => '4071',    2 => 855,    'part_count' => 855,  ),  3 =>   array (    0 => '237033AW0A',    'cust_no' => '237033AW0A',    1 => '4409',    'lotno' => '4409',    2 => 300,    'part_count' => 300,  ),  4 =>   array (    0 => '237033AW0A',    'cust_no' => '237033AW0A',    1 => '4410',    'lotno' => '4410',    2 => 141,    'part_count' => 141,  ),  5 =>   array (    0 => '237033RC3A ',    'cust_no' => '237033RC3A ',    1 => '4418',    'lotno' => '4418',    2 => 80,    'part_count' => 80,  ),  6 =>   array (    0 => '237033TC0A',    'cust_no' => '237033TC0A',    1 => '4401',    'lotno' => '4401',    2 => 2,    'part_count' => 2,  ),  7 =>   array (    0 => '237033TC0A',    'cust_no' => '237033TC0A',    1 => '4402',    'lotno' => '4402',    2 => 1,    'part_count' => 1,  ),  8 =>   array (    0 => '237033TC0A',    'cust_no' => '237033TC0A',    1 => '4408',    'lotno' => '4408',    2 => 178,    'part_count' => 178,  ),  9 =>   array (    0 => '237033TC0A',    'cust_no' => '237033TC0A',    1 => '4409',    'lotno' => '4409',    2 => 374,    'part_count' => 374,  ),)//数组barray (  0 =>   array (    'cust_no' => '237033AW0A',    'total' => 375,  ),  1 =>   array (    'cust_no' => '237033RC3A',    'total' => 75,  ),)


数组a与数组b匹配,当数组a的cust_no和数量满足数组b的cust_no和数量时。需要$ress求得的类似结果如下:

Array(    [0] => Array        (            [cust_no] => 237033AW0A            [lotno] => 4409            [part_count] => 300        )    [1] => Array        (            [cust_no] => 237033AW0A            [lotno] => 4410            [part_count] => 75        )    [2] => Array        (            [cust_no] => 237033RC3A            [lotno] => 4418            [part_count] => 75        ))


这样的话可否实现?或者放在如1#的循环中,当数组b值为多少时,对应的数组a的匹配结果为多少?$ress值被赋值后如何重置?

是这个意思?还是有进一步要求?

$a = array (  0 =>   array (    'cust_no' => '12653627',    'lotno' => '4069',    'part_count' => 19,  ),  1 =>   array (    'cust_no' => '12653627',    'lotno' => '4071',    'part_count' => 146,  ),  2 =>   array (    'cust_no' => '12654172',    'lotno' => '4071',    'part_count' => 855,  ),  3 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4409',    'part_count' => 300,  ),  4 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4410',    'part_count' => 141,  ),  5 =>   array (    'cust_no' => '237033RC3A',    'lotno' => '4418',    'part_count' => 80,  ),  6 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4401',    'part_count' => 2,  ),  7 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4402',    'part_count' => 1,  ),  8 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4408',    'part_count' => 178,  ),  9 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4409',    'part_count' => 374,  ),); $b = array (  0 =>   array (    'cust_no' => '237033AW0A',    'total' => 375,  ),  1 =>   array (    'cust_no' => '237033RC3A',    'total' => 75,  ),);foreach($b as $d) {  $k = $d['cust_no'];  reset($a);  foreach($a as $r) {    if($k != $r['cust_no']) continue;    $res[] = $r;  }}print_r($res);
Array
(
[0] => Array
(
[cust_no] => 237033AW0A
[lotno] => 4409
[part_count] => 300
)

[1] => Array
(
[cust_no] => 237033AW0A
[lotno] => 4410
[part_count] => 141
)

[2] => Array
(
[cust_no] => 237033RC3A
[lotno] => 4418
[part_count] => 80
)

)

是这个意思?还是有进一步要求?


$a的数据对应#5的数据。

$v1){	$sql2 = "select cust_no,total=SUM(mount)*snp from ".$t." where po_num = '".$stockno."' and cust_no = '".$v1['cust_no']."' group by cust_no,snp";		$result2 = mssql_query($sql2);	$b = array();	while($row2 = mssql_fetch_array($result2))	{		$b[] = $row2;	}		var_export($b);	/*数组b分别对应2笔数据		数据1:  array ( 0 => array ( 0 => '237033AW0A', 'cust_no' => '237033AW0A', 1 => 375, 'total' => 375, ),)	数据2: array ( 0 => array ( 0 => '237033RC3A', 'cust_no' => '237033RC3A', 1 => 75, 'total' => 75, ), )) 		*/									foreach($b as $var)	{		$num = $var['total'];		foreach($a as $i=>$dest)		{			if($num == 0)				break;			if($dest['cust_no'] != $var['cust_no'])				continue;			if($num >= $dest['part_count'])			{				$num -= $dest['part_count'];				$ress[] = $dest;				$a[$i]['part_count'] = 0;			}			else			{				$dest['part_count'] = $num;				$ress[] = $dest;				$a[$i]['part_count'] -= $num;				$num = 0;			}		}	}	print_r($ress);		/*	目前的结果:		第一笔:	Array	(		[0] => Array			(				[0] => 237033AW0A				[cust_no] => 237033AW0A				[1] => 4409				[lotno] => 4409				[2] => 300				[part_count] => 300			)		[1] => Array			(				[0] => 237033AW0A				[cust_no] => 237033AW0A				[1] => 4410				[lotno] => 4410				[2] => 141				[part_count] => 75			)	)		第二笔:	Array	(		[0] => Array			(				[0] => 237033AW0A				[cust_no] => 237033AW0A				[1] => 4409				[lotno] => 4409				[2] => 300				[part_count] => 300			)		[1] => Array			(				[0] => 237033AW0A				[cust_no] => 237033AW0A				[1] => 4410				[lotno] => 4410				[2] => 141				[part_count] => 75			)	)			希望得到的数据:	第一笔:		Array ( 			[0] => Array ( 							[cust_no] => 237033AW0A 							[lotno] => 4409 							[part_count] => 300 						) 			[1] => Array ( 							[cust_no] => 237033AW0A 							[lotno] => 4410 							[part_count] => 75 						  ) 		   )		第二笔:		Array (			[0] => Array (							[cust_no] => 237033RC3A							[lotno] => 4418							[part_count] => 75						  )		  )		*/}?>

部分数据补充:

$arr = array ( 0 => array ( 0 => '2014-03-19 11:00', 'over_time' => '2014-03-19 11:00', 1 => 'A2', 'arr_adress' => 'A2', 2 => 'L12F', 'c_type' => 'L12F', 3 => 'SU14031300627', 'po_num' => 'SU14031300627', 4 => '237033AW0A', 'cust_no' => '237033AW0A', 5 => 25, 'mount' => 25, 6 => 15, 'snp' => 15, 7 => 375, 'totals' => 375, 8 => '2014-03-15', 'late_time' => '2014-03-15', ), 1 => array ( 0 => '2014-03-19 11:00', 'over_time' => '2014-03-19 11:00', 1 => 'A2', 'arr_adress' => 'A2', 2 => 'L12F', 'c_type' => 'L12F', 3 => 'SU14031300627', 'po_num' => 'SU14031300627', 4 => '237033RC3A', 'cust_no' => '237033RC3A', 5 => 5, 'mount' => 5, 6 => 15, 'snp' => 15, 7 => 75, 'totals' => 75, 8 => '2014-03-15', 'late_time' => '2014-03-15', ), ) $a = array (  0 =>   array (    'cust_no' => '12653627',    'lotno' => '4069',    'part_count' => 19,  ),  1 =>   array (    'cust_no' => '12653627',    'lotno' => '4071',    'part_count' => 146,  ),  2 =>   array (    'cust_no' => '12654172',    'lotno' => '4071',    'part_count' => 855,  ),  3 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4409',    'part_count' => 300,  ),  4 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4410',    'part_count' => 141,  ),  5 =>   array (    'cust_no' => '237033RC3A',    'lotno' => '4418',    'part_count' => 80,  ),  6 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4401',    'part_count' => 2,  ),  7 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4402',    'part_count' => 1,  ),  8 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4408',    'part_count' => 178,  ),  9 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4409',    'part_count' => 374,  ),);

我 #6 的不符合你的要求吗?
如果是要对 $b 分组对待,则
foreach($b as $d) {
$res = array();

我 #6 的不符合你的要求吗?
如果是要对 $b 分组对待,则
foreach($b as $d) {
$res = array();



需要对$a进行减处理,以满足$b的结果。例如:
$ress的结果
Array ( [0] => Array ( [cust_no] => 237033AW0A [lotno] => 4409 [part_count] => 300 ) [1] => Array ([cust_no] => 237033AW0A [lotno] => 4410 [part_count] => 75 ) )
满足$b的结果
array ( 0 => array ( 'cust_no' => '237033AW0A', 'total' => 375, ), )

我 #6 的不符合你的要求吗?
如果是要对 $b 分组对待,则
foreach($b as $d) {
$res = array();

我 #6 的不符合你的要求吗?


如果是求当$a和$b的cust_no字段相同时,$a的part_count的数量满足$b的total的数量怎么求?

我 #6 的不符合你的要求吗?



link

和这个问题类似。不过$b变成了多维数组。

在那个帖子中 $b 也是多维数组

在那个帖子中 $b 也是多维数组


但是只能求得$b中第一个一维数组的结果集。

这个意思?

$a = array (  0 =>   array (    'cust_no' => '12653627',    'lotno' => '4069',    'part_count' => 19,  ),  1 =>   array (    'cust_no' => '12653627',    'lotno' => '4071',    'part_count' => 146,  ),  2 =>   array (    'cust_no' => '12654172',    'lotno' => '4071',    'part_count' => 855,  ),  3 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4409',    'part_count' => 300,  ),  4 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4410',    'part_count' => 141,  ),  5 =>   array (    'cust_no' => '237033RC3A',    'lotno' => '4418',    'part_count' => 80,  ),  6 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4401',    'part_count' => 2,  ),  7 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4402',    'part_count' => 1,  ),  8 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4408',    'part_count' => 178,  ),  9 =>   array (    'cust_no' => '237033TC0A',    'lotno' => '4409',    'part_count' => 374,  ),);  $b = array (  array (    'cust_no' => '237033AW0A',    'total' => 375,  ),  array (    'cust_no' => '237033RC3A',    'total' => 75,  ),); foreach($b as $source) {  do {    $k = 0;    $num = $source['total'];    foreach($a as $i=>$dest) {      if($num == 0) break;      if($dest['cust_no'] != $source['cust_no']) continue;      if($dest['part_count'] == 0) continue;      $k++;      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;      }    }  }while($k);}var_export($res);
array (  0 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4409',    'part_count' => 300,  ),  1 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4410',    'part_count' => 75,  ),  2 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4410',    'part_count' => 66,  ),  3 =>   array (    'cust_no' => '237033RC3A',    'lotno' => '4418',    'part_count' => 75,  ),  4 =>   array (    'cust_no' => '237033RC3A',    'lotno' => '4418',    'part_count' => 5,  ),)

这个意思?



如果只求得这样的结果集:怎么求?

array (  0 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4409',    'part_count' => 300,  ),  1 =>   array (    'cust_no' => '237033AW0A',    'lotno' => '4410',    'part_count' => 75,  ),  2 =>   array (    'cust_no' => '237033RC3A',    'lotno' => '4418',    'part_count' => 75,  ),)

这个意思?



可能是哪里的程序问题,同样的代码,程序的和例子得出来的结果不一样。我先调试调试再说吧。谢谢!

人气教程排行