数组文件操作
$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.'
'; } else { $sql2 = "insert into `".$o."`(cust_no,lotno,count) values ('".$tts['cust_no']."','".$tts['lotno']."','".$tts['count']."')"; echo $sql2.'
'; } } }//结果是这样的: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";}