数组文件操作

php中文网
发布: 2016-06-23 14:21:02
原创
1007人浏览过

本帖最后由 lazygc520 于 2013-09-05 13:13:44 编辑

已知数组a和数组b
$a = array ( 0 =>   array (    'cust_no' => '310F6 1VA5A',    'lotno' => '2X15',    'count' => '0',  ),  1 =>   array (    'cust_no' => '310F6 1VA5A',    'lotno' => '2Z15',    'count' => '16',  ),);$b = array (  0 =>   array (    'cust_no' => '310F6 1VA5A',    'lotno' => '2X15',    'count' => '0',  ),  1 =>   array (    'cust_no' => '310F6 1VA5A',    'lotno' => '2Z15',    'count' => '32',  ),  2 =>   array (    'cust_no' => '310F6 1VA5A',    'lotno' => '3528',    'count' => '80',  ),);
登录后复制


想要得到的结果是当数组b和数组a的cust_no和lotno都相同时,则提示update table sql
否则insert table sql。请问怎么求?

回复讨论(解决方案)

你这是二维啊,谁跟谁相同?

你这是二维啊,谁跟谁相同?
我用foreach来遍历2个2维数组,就例中而言,数组b存在: 'cust_no' => '310F6 1VA5A', 'lotno' => '2X15'
数组a也存在: 'cust_no' => '310F6 1VA5A', 'lotno' => '2X15'。因为对应的cust_no和lotno键值都相等,所以进行update操作。

foreach ($a as $a1) {
    foreach ($b as $b1) {
        if $a1['cust_no'] == $b1['cust_no'] && $a1['lotno'] == $b1['lotno'] {
        ...

你都四角了,我感觉你这帖子是在开玩笑吧?

$a = array ( 0 =>   array (    'cust_no' => '310F6 1VA5A',    'lotno' => '2X15',    'count' => '0',  ),  1 =>   array (    'cust_no' => '310F6 1VA5A',    'lotno' => '2Z15',    'count' => '16',  ),); $b = array (  0 =>   array (    'cust_no' => '310F6 1VA5A',    'lotno' => '2X15',    'count' => '0',  ),  1 =>   array (    'cust_no' => '310F6 1VA5A',    'lotno' => '2Z15',    'count' => '32',  ),  2 =>   array (    'cust_no' => '310F6 1VA5A',    'lotno' => '3528',    'count' => '80',  ),);foreach($b as $i=>$v) {  print_r($v);  if(isset($a[$i]) && count(array_intersect_assoc($v, $a[$i])) == count($v)) {     echo "update table sql\n";  }else echo "insert table sql\n";}
登录后复制
Array
(
    [cust_no] => 310F6 1VA5A
    [lotno] => 2X15
    [count] => 0
)
update table sql
Array
(
    [cust_no] => 310F6 1VA5A
    [lotno] => 2Z15
    [count] => 32
)
insert table sql
Array
(
    [cust_no] => 310F6 1VA5A
    [lotno] => 3528
    [count] => 80
)
insert table sql

你都四角了,我感觉你这帖子是在开玩笑吧?
可能我表述不清,结果不是这样的。

foreach($bb as $cc)			{				foreach($tt as $tts)				{					if(($tts['cust_no'] == $cc['cust_no'])&&($tts['lotno'] == $cc['lotno']))					{						$sql = "update `".$o."` set count = '".$tts['count']."' where cust_no = '".$tts['cust_no']."' and lotno = '".$tts['lotno']."'";						echo $sql.'<br />';					}					else					{						$sql2 = "insert into `".$o."`(cust_no,lotno,count) values ('".$tts['cust_no']."','".$tts['lotno']."','".$tts['count']."')";						echo $sql2.'<br />';					}				}			}//结果是这样的:update `kw_mount` set count = '0' where cust_no = '310F6 1VA5A' and lotno = '2X15'insert into `kw_mount`(cust_no,lotno,count) values ('310F6 1VA5A','2Z15','32')insert into `kw_mount`(cust_no,lotno,count) values ('310F6 1VA5A','3528','80')insert into `kw_mount`(cust_no,lotno,count) values ('310F6 1VA5A','2X15','0')update `kw_mount` set count = '32' where cust_no = '310F6 1VA5A' and lotno = '2Z15'insert into `kw_mount`(cust_no,lotno,count) values ('310F6 1VA5A','3528','80')//而我想得到的结果是update `kw_mount` set count = '0' where cust_no = '310F6 1VA5A' and lotno = '2X15'update `kw_mount` set count = '32' where cust_no = '310F6 1VA5A' and lotno = '2Z15'insert into `kw_mount`(cust_no,lotno,count) values ('310F6 1VA5A','3528','80')
登录后复制


这样要怎么求?

Array
(
    [cust_no] => 310F6 1VA5A
    [lotno] => 2X15
    [count] => 0
)
update table sql
Array
(
    [cust_no] => 310F6 1VA5A
    [lotno] => 2Z15
    [count] => 32
)
insert table sql 
Array
(
    [cust_no] => 310F6 1VA5A
    [lotno] => 3528
    [count] => 80
)
insert table sql
Array
(
    [cust_no] => 310F6 1VA5A
    [lotno] => 2X15
    [count] => 0
)
update table sql
Array
(
    [cust_no] => 310F6 1VA5A
    [lotno] => 2Z15
    [count] => 32
)
insert table sql //这里要变成update table sql,因为cust_no和lotno都相同
Array
(
    [cust_no] => 310F6 1VA5A
    [lotno] => 3528
    [count] => 80
)
insert table sql

要什么条件自己改

foreach($b as $i=>$v) {  print_r($v);  if(isset($a[$i]) && $v['cust_no'] == $a[$i]['cust_no'] && $v['lotno'] == $a[$i]['lotno']) {     echo "update table sql\n";  }else echo "insert table sql\n";}
登录后复制

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号