2数组是前台提交的JSON编码后的字符串,在后台经过json_decode函数反编码得到的数组
数组1 old_data
Array
(
[0] => stdClass Object
(
[row] => 1
[cell] => 1
[qty] => 5
[isMake] => 1
)
[1] => stdClass Object
(
[row] => 1
[cell] => 2
[qty] => 50
[isMake] => 0
)
)
数组2 new_data
Array
(
[0] => stdClass Object
(
[row] => 1
[cell] => 1
[qty] => 5
[isMake] => 1
)
[1] => stdClass Object
(
[row] => 1
[cell] => 2
[qty] => 100
[isMake] => 0
)
[2] => stdClass Object
(
[row] => 1
[cell] => 3
[qty] => 50
[isMake] => 0
)
)数组中,row和cell是无法在前台更改的
如果isMake=1的话,则不会再进行修改,无须再进行比较
只比较isMake=0的,比较的话就比较是否完全一致,只要有不一致的地方,则这个old_data中的元素就比较update
如果new_data中的元素在old_data中找不到,则insert
如果old_data中的元素在new_data中找不到,则delete有什么办法比较快吗??
我的土方法代码$update_arr = new Array();
$delete_arr = $old_data;
for($i = 0; $i < count($old_data); $i++) {
if($old_data[$i]['isMake']) {
break;
} else {
$tmp_old = join(",",$old_data[$i]);
for($j = 0; $j < count($new_data); $j++) {
if(($old_data[$i]['row'] === $new_data[$j]['row']) && ($old_data[$i]['cell'] === $new_data[$j]['cell'])) {
$tmp_new = join(",",$new_data[j]);
if($tmp_old === $tmp_new) {
unset($delete_arr[$i]);
unset($new_data[$j]);
break;
} else {
array_push($update_arr, $new_data[$j]);
unset($delete_arr[$i]);
unset($new_data[$j]);
break;
}
}
}
}
}
$insert_arr = $new_data;
数组1 old_data
Array
(
[0] => stdClass Object
(
[row] => 1
[cell] => 1
[qty] => 5
[isMake] => 1
)
[1] => stdClass Object
(
[row] => 1
[cell] => 2
[qty] => 50
[isMake] => 0
)
)
数组2 new_data
Array
(
[0] => stdClass Object
(
[row] => 1
[cell] => 1
[qty] => 5
[isMake] => 1
)
[1] => stdClass Object
(
[row] => 1
[cell] => 2
[qty] => 100
[isMake] => 0
)
[2] => stdClass Object
(
[row] => 1
[cell] => 3
[qty] => 50
[isMake] => 0
)
)数组中,row和cell是无法在前台更改的
如果isMake=1的话,则不会再进行修改,无须再进行比较
只比较isMake=0的,比较的话就比较是否完全一致,只要有不一致的地方,则这个old_data中的元素就比较update
如果new_data中的元素在old_data中找不到,则insert
如果old_data中的元素在new_data中找不到,则delete有什么办法比较快吗??
我的土方法代码$update_arr = new Array();
$delete_arr = $old_data;
for($i = 0; $i < count($old_data); $i++) {
if($old_data[$i]['isMake']) {
break;
} else {
$tmp_old = join(",",$old_data[$i]);
for($j = 0; $j < count($new_data); $j++) {
if(($old_data[$i]['row'] === $new_data[$j]['row']) && ($old_data[$i]['cell'] === $new_data[$j]['cell'])) {
$tmp_new = join(",",$new_data[j]);
if($tmp_old === $tmp_new) {
unset($delete_arr[$i]);
unset($new_data[$j]);
break;
} else {
array_push($update_arr, $new_data[$j]);
unset($delete_arr[$i]);
unset($new_data[$j]);
break;
}
}
}
}
}
$insert_arr = $new_data;
解决方案 »
- XAMPP 使用中pear
- 请教一个PHP调用MYSQL存储过程的问题(请看这个)
- 有一.htm文件我想做修改 可是发现无法修改不知为什么?
- php用iconv转码问题,添加了参数//IGNORE,但还是转到一半就断了!!求解
- 如何在显示一些信息后,停顿一段时间,再返回原页面?
- 关于time()的一个大问题,请各位帮帮忙
- 傻问题(在php5中,怎么打印出一个对象)
- 我要购买一个空间让大家帮忙推存一下!
- 请问一个正则表达式的匹配
- 在用ASP或者PHP生成页面的时候,我一直是担心网页打开速度慢!有人能给我解释一下吗?清楚者重分!
- file_get_contents函数有时候可用,有是有提示连接超时
- checkbox未选中的不提交,我想提交所有checkbox未选中的值为false,选中的为true,能不能实现
$delete_arr = $old_data;
for($i = 0; $i < count($old_data); $i++) {
if($old_data[$i]['isMake']) {
unset($delete_arr[$i]);
unset($new_data[$j]);
break;
} else {
$tmp_old = join(",",$old_data[$i]);
for($j = 0; $j < count($new_data); $j++) {
if(($old_data[$i]['row'] === $new_data[$j]['row']) && ($old_data[$i]['cell'] === $new_data[$j]['cell'])) {
$tmp_new = join(",",$new_data[j]);
if($tmp_old === $tmp_new) {
unset($delete_arr[$i]);
unset($new_data[$j]);
break;
} else {
array_push($update_arr, $new_data[$j]);
unset($delete_arr[$i]);
unset($new_data[$j]);
break;
}
}
}
}
}
$insert_arr = $new_data;
又看了一遍,第一个判断那边还是有错误...unset($new_data[$j]),此时根本没有$j...
如果用$i也不行,因为顺序可能变化的...再改...$update_arr = new Array();
$delete_arr = $old_data;
for($i = 0; $i < count($old_data); $i++) {
if($old_data[$i]['isMake']) {
unset($delete_arr[$i]);
for($j = 0; $j < count($new_data); $j++) {
if(($old_data[$i]['row'] === $new_data[$j]['row']) && ($old_data[$i]['cell'] === $new_data[$j]['cell'])) {
unset($new_data[$j]);
break;
}
}
break;
} else {
$tmp_old = join(",",$old_data[$i]);
for($j = 0; $j < count($new_data); $j++) {
if(($old_data[$i]['row'] === $new_data[$j]['row']) && ($old_data[$i]['cell'] === $new_data[$j]['cell'])) {
$tmp_new = join(",",$new_data[j]);
if($tmp_old === $tmp_new) {
unset($delete_arr[$i]);
unset($new_data[$j]);
break;
} else {
array_push($update_arr, $new_data[$j]);
unset($delete_arr[$i]);
unset($new_data[$j]);
break;
}
}
}
}
}
$insert_arr = $new_data;
然后看下是否存在你的问题,应该很多人都遇到过,就是删除某个内容,又添加某个内容
你可以使用ajax方式,删除一个就提交一个添加一个就提交一个queryphp网站自动生成HTML,从SEO优化中解脱出来
-------------------------------------------------
http://topic.csdn.net/u/20100331/21/0ac266ec-1910-483b-9f91-4d781a773b96.html
我在前台展示的是这样一个TABLE
+----+-------+---+---+---+----+
| ID | COLOR | S | M | L | XL |
+----+-------+---+---+---+----+
| _1 | RED__ | _ | _ | 9 | 49 |
+----+-------+---+---+---+----+
| _2 | BLUE_ | 4 | 7 | _ | 44 |
+----+-------+---+---+---+----+
因为S、M、L、XL这写字段并不是固定的,少的时候只有一个,多的时候有10几个SIZE字段
所以我在后台数据库是使用类似
+-----+------+-------+-----+--------+
| ROW | CELL | COLOR | QTY | ISMAKE |
+-----+------+-------+-----+--------+
| _1_ | __3_ | RED__ | _9_ | ____0_ |
+-----+------+-------+-----+--------+
| _1_ | __4_ | RED__ | _49 | ____0_ |
+-----+------+-------+-----+--------+
| _2_ | __1_ | BLUE_ | _4_ | ____0_ |
+-----+------+-------+-----+--------+
| _2_ | __2_ | BLUE_ | _7_ | ____0_ |
+-----+------+-------+-----+--------+
| _2_ | __4_ | BLUE_ | _44 | ____0_ |
+-----+------+-------+-----+--------+
这样来存储这个表格数据的(之后还要根据QTY字段和ISMAKE字段进行一系列操作)所以如果是更改一个数据就进行一次数据库操作的话,那太频繁了点吧...
$update_arr = array();
$delete_arr = $old_data = json_decode($_POST['old_json']);
$new_data = json_decode($_POST['new_json']);for($i = 0; $i < count($old_data); $i++) {
if($old_data[$i]->ismake) {
unset($delete_arr[$i]);
for($j = 0; $j < count($new_data); $j++) {
if((($old_data[$i]->ROW) == ($new_data[$j]->ROW)) && (($old_data[$i]->CELL) == ($new_data[$j]->CELL))) {
array_splice($new_data,$j,1);
break;
}
}
break;
} else {
$tmp_old = implode('|',(array)$old_data[$i]);
for($j = 0; $j < count($new_data); $j++) {
if((($old_data[$i]->ROW) == ($new_data[$j]->ROW)) && (($old_data[$i]->CELL) == ($new_data[$j]->CELL))) {
$tmp_new = implode('|',(array)$new_data[$j]);
if($tmp_old === $tmp_new) {
unset($delete_arr[$i]);
array_splice($new_data,$j,1);
break;
} else {
array_push($update_arr, $new_data[$j]);
unset($delete_arr[$i]);
array_splice($new_data,$j,1);
break;
}
}
}
}
}
$insert_arr = $new_data;这是我最终修改后的代码...测试可以达到预期的效果了...但总觉得应该可以再简单点...
$update_arr = array();
$delete_arr = $old_data = json_decode($_POST['old_json']);
$new_data = json_decode($_POST['new_json']);for($i = 0; $i < count($old_data); $i++) {
if($old_data[$i]->ismake) {
unset($delete_arr[$i]);
for($j = 0; $j < count($new_data); $j++) {
if((($old_data[$i]->ROW) == ($new_data[$j]->ROW)) && (($old_data[$i]->CELL) == ($new_data[$j]->CELL))) {
array_splice($new_data,$j,1);
break;
}
}
} else {
$tmp_old = implode('|',(array)$old_data[$i]);
for($j = 0; $j < count($new_data); $j++) {
if((($old_data[$i]->ROW) == ($new_data[$j]->ROW)) && (($old_data[$i]->CELL) == ($new_data[$j]->CELL))) {
$tmp_new = implode('|',(array)$new_data[$j]);
if($tmp_old === $tmp_new) {
unset($delete_arr[$i]);
array_splice($new_data,$j,1);
break;
} else {
array_push($update_arr, $new_data[$j]);
unset($delete_arr[$i]);
array_splice($new_data,$j,1);
break;
}
}
}
}
}
$insert_arr = $new_data;