讲师中心 微信公众号

工具推荐

视频效率加速
Seko
Seko

AI短剧一键生成成片

SoundView
SoundView

字幕擦除翻译、配音口型同步

立刻MV
立刻MV

低成本歌曲 MV、视频生成

updream

一键生成高质量推广成片

将变量数组重置

冬枫姑娘_6929

冬枫姑娘_6929

发布时间:2016-06-23 13:58:30

|

1080人浏览过

|

来源于php中文网

原创

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的数据。

<?phpforeach($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;	}		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,  ),)

这个意思?



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

热门AI工具

更多
讯飞智作

讯飞智作是一款AI视频创作工具,AI文本配音工具,数字人课程、营销视频制作。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

小橡皮AI

一款AI工具,主要用于小橡皮一键去AI味,将机器味重的内容改得更有活人感,防止因为 AI 内容被平台打上 AI 标签,以及限流,适合需要提升相关任务效率的用户。

Loomy
Loomy Hot

一款AI工具,主要用于科大讯飞发布的桌面级 AI 助理,比 OpenClaw 更易用、更安全!,适合需要提升相关任务效率的用户。

DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

Seko
Seko Hot

一款AI视频创作工具,主要用于商汤科技推出的创编一体的AI短视频创作Agent,适合需要提升相关任务效率的用户。

墨刀AI
墨刀AI Hot

一款AI图像与设计工具,主要用于产品经理的专属智能体,适合需要提升相关任务效率的用户。

蛙蛙写作

一款AI论文写作工具,主要用于超级AI智能写作助手,适合需要提升相关任务效率的用户。

相关专题

更多
Navicat创建数据库教程
Navicat创建数据库教程

本专题整理Navicat创建数据库相关教程,介绍新建前的准备、基础设置和创建后的检查方法。内容兼顾入门学习及实际使用需求,帮助用户了解不同数据库类型下的操作差异,为后续建表、保存数据和日常管理提供参考。

0

2026.11.20

Navicat入门教程大全
Navicat入门教程大全

本专题整理Navicat入门教程,从认识软件界面到开展数据库操作,介绍初次使用需要了解的知识和方法。内容兼顾连接配置、数据管理及日常使用中的疑问,方便用户按学习进度查阅,为后续实践和独立操作打好基础。

0

2026.11.20

Navicat数据库连接配置指南
Navicat数据库连接配置指南

从确认数据库信息到建立可用连接,本专题串联Navicat连接数据库的相关知识和操作方法。既有初次配置时需要了解的基础内容,也有连接后的检查思路,方便用户按实际需求查阅教程,逐步熟悉数据库连接的设置流程。

0

2026.11.20

PHP8.6数组与数据处理教程
PHP8.6数组与数据处理教程

本专题汇总PHP8.6数组与数据处理教程,涵盖CSV读取和导出、引用遍历删除、array_filter回调模式、配置键名冲突、pathinfo文件名校验、GMP大整数进制转换及UTF-8正则筛选,说明升级兼容变化、输入范围和失败处理方法。

0

2026.11.20

PHP8.6升级迁移教程
PHP8.6升级迁移教程

本专题汇总PHP8.6升级迁移与弃用接口改造教程,涵盖反射调用、对象属性赋值、标识符更名、构造函数与__debugInfo调整,以及插件类名检查、常量导出、ArrayIterator排序、SPL和DTO查询参数转换,帮助梳理弃用提示并完成代码兼容改造。

0

2026.11.20

PHP8.6安装配置与FPM运维教程
PHP8.6安装配置与FPM运维教程

本专题汇总PHP8.6正式版安装部署与FPM运维教程,涵盖Linux并存安装、独立ini配置、扩展重新编译、Nginx切换与回滚、FPM状态监控和慢日志排查,以及环境变量、请求超时、OPcache刷新和预加载配置。

0

2026.11.20

Nginx CVE-2026-123007排查与防护指南
Nginx CVE-2026-123007排查与防护指南

Nginx HTTP/2漏洞该怎么修复?本专题围绕CVE-2026-123007,介绍安全版本升级、模块兼容检查和配置恢复方法,以及日志排查、权限设置等后续加固操作,方便你按实际部署情况查找对应教程。

20

2026.11.20

GoFrame框架Web开发与路由中间件
GoFrame框架Web开发与路由中间件

本专题聚焦GoFrame框架的Web开发,拆解路由注册与匹配规则、请求参数获取、参数校验与中间件执行顺序,说明统一响应、JWT鉴权、文件上传与接口文档生成的落地方式,并补充优雅重启与测试实践。

80

2026.11.17

GoFrame框架安装与gf命令行工具
GoFrame框架安装与gf命令行工具

本专题围绕GoFrame框架的安装与工具链配置展开,讲清Go版本要求、gf命令行的安装方式、代理镜像与依赖准备,说明项目初始化、热编译、交叉编译打包的做法,并整理安装阶段的常见报错与验证步骤。

80

2026.11.17

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Navicat 18 macOS用户手册
Navicat 18 macOS用户手册

共0课时 | 0人学习

Docker Compose使用手册
Docker Compose使用手册

共0课时 | 0人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn