数组文件操作

WBOY
Lepaskan: 2016-06-23 14:21:02
asal
945 orang telah melayarinya

本帖最后由 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',  ),);
Salin selepas log masuk


想要得到的结果是当数组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";}
Salin selepas log masuk
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')
Salin selepas log masuk


这样要怎么求?

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";}
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan