PHP如何分离序列号(数字范围)
我想分开数字,比如序列号,例如: 我有PHP如何分离序列号(数字范围),php,arrays,function,class,pdo,Php,Arrays,Function,Class,Pdo,我想分开数字,比如序列号,例如: 我有1-1000 如果我想删除1-100,那么我将得到101-1000 如果我想删除901-1000,那么我将得到1-900 如果我想删除101-200,那么我将得到1-100和201-1000 等等 我只是插入第一个数字和最后一个数字,比如1和100 问题是,我使用MySQL存储数据,这是我的表: [items(id,name), serials(id,name), details(id,item_id,serial_id,first,last)][1]
1-1000
- 如果我想删除
,那么我将得到1-100
101-1000
- 如果我想删除
,那么我将得到901-1000
1-900
- 如果我想删除
,那么我将得到101-200
和1-100
201-1000
- 等等
[items(id,name), serials(id,name), details(id,item_id,serial_id,first,last)][1]
我有一个解决方案,但我认为我的解决方案太长,对于我的代码来说效率不高。如果我处理大数据,这就是我的解决方案
public function checkSerialNum(array $data){
$item_id = $data['item_id'];
$serial_id = $data['serial_id'];
$first = $data['first'];
$last = $data['last'];
if($first > $last) return false;
$sql = "SELECT * FROM details WHERE item_id = '$item_id' AND serial_id = '$serial_id'";
$details = $this->db->run($sql)->fetchAll();
$new_detail = [];
foreach($details as $key => $detail){
if($first == $detail->first && $last == $detail->last){ // 1 - 1000 -> 0 - 0
$new_detail['item_id'][] = $item_id;
$new_detail['serial_id'][] = $serial_id;
$new_detail['first'][] = 0;
$new_detail['last'][] = 0;
$new_detail['old_id'][] = $detail->id;
}elseif($first > $detail->first && $last == $detail->last){ // 901-1000 -> 1 - 900
$new_detail['item_id'][] = $item_id;
$new_detail['serial_id'][] = $serial_id;
$new_detail['first'][] = $detail->first;
$new_detail['last'][] = $first - 1;
$new_detail['old_id'][] = $detail->id;
}elseif($first == $detail->first && $last < $detail->last){ // 1 - 100 -> 101 - 1000
$new_detail['item_id'][] = $item_id;
$new_detail['serial_id'][] = $serial_id;
$new_detail['first'][] = $last + 1;
$new_detail['last'][] = $detail->last;
$new_detail['old_id'][] = $detail->id;
}elseif($first > $detail->first && $last < $detail->last){ //101-200 -> 1-100 & 201-1000
$new_detail['item_id'][] = $item_id;
$new_detail['serial_id'][] = $serial_id;
$new_detail['first'][] = $detail->first;
$new_detail['last'][] = $first - 1;
$new_detail['old_id'][] = $detail->id;
$new_detail['item_id'][] = $item_id;
$new_detail['serial_id'][] = $serial_id;
$new_detail['first'][] = $last + 1;
$new_detail['last'][] = $detail->last;
}
}
return $this->tidyArray($new_detail);
}
public function tidyArray($arr){
$new_arr = [];
foreach($arr as $k => $array){
for($i=0;$i < count($array); $i++){
$new_arr[$i][$k] = $array[$i];
}
}
return $new_arr;
}
public function removeSerialNum(array $data){ //
$new_details = $this->checkSerialNum($data);
if(!$new_details) return false;
$item_id = $data['item_id'];
$serial_id = $data['serial_id'];
foreach($new_details as $nd){
if(isset($nd['old_id'])){
$old_id = $nd['old_id'];
$sql = "DELETE FROM details WHERE item_id = '$item_id' AND serial_id = '$serial_id' AND id = '$old_id'";
$this->db->run($sql);
unset($nd['old_id']);
}
$this->insertData('details',$nd);
}
return true;
}
}
函数tidyArray()
正在更改格式数组,如下所示:
////// it will return like this if I'm not breaking any range, like I've 821-900 and I just input 821-850
Array
(
[item_id] => Array
(
[0] => 1
)
[serial_id] => Array
(
[0] => 1
)
[first] => Array
(
[0] => 821
)
[last] => Array
(
[0] => 850
)
[old_id] => Array
(
[0] => 8
)
)
/////but if I've 821-900 and input like 851-860, it will return like this
Array
(
[item_id] => Array
(
[0] => 1
[1] => 1
)
[serial_id] => Array
(
[0] => 1
[1] => 1
)
[first] => Array
(
[0] => 821
[1] => 861
)
[last] => Array
(
[0] => 850
[1] => 900
)
[old_id] => Array
(
[0] => 10
)
)
//////it will return like this if I'm not breaking any range, like I've 821-900 and I just input 821-850
Array
(
[0] => Array
(
[item_id] => 1
[serial_id] => 1
[first] => 811
[last] => 900
[old_id] => 8
)
)
/////but if I've 821-900 and input like 851-860, it will return like this, its like new range of data, I will explain old_id below..
Array
(
[0] => Array
(
[item_id] => 1
[serial_id] => 1
[first] => 821
[last] => 850
[old_id] => 10
)
[1] => Array
(
[item_id] => 1
[serial_id] => 1
[first] => 861
[last] => 900
)
)
对这样的事情:
////// it will return like this if I'm not breaking any range, like I've 821-900 and I just input 821-850
Array
(
[item_id] => Array
(
[0] => 1
)
[serial_id] => Array
(
[0] => 1
)
[first] => Array
(
[0] => 821
)
[last] => Array
(
[0] => 850
)
[old_id] => Array
(
[0] => 8
)
)
/////but if I've 821-900 and input like 851-860, it will return like this
Array
(
[item_id] => Array
(
[0] => 1
[1] => 1
)
[serial_id] => Array
(
[0] => 1
[1] => 1
)
[first] => Array
(
[0] => 821
[1] => 861
)
[last] => Array
(
[0] => 850
[1] => 900
)
[old_id] => Array
(
[0] => 10
)
)
//////it will return like this if I'm not breaking any range, like I've 821-900 and I just input 821-850
Array
(
[0] => Array
(
[item_id] => 1
[serial_id] => 1
[first] => 811
[last] => 900
[old_id] => 8
)
)
/////but if I've 821-900 and input like 851-860, it will return like this, its like new range of data, I will explain old_id below..
Array
(
[0] => Array
(
[item_id] => 1
[serial_id] => 1
[first] => 821
[last] => 850
[old_id] => 10
)
[1] => Array
(
[item_id] => 1
[serial_id] => 1
[first] => 861
[last] => 900
)
)
最后一个是函数removeSerialNum()
这将删除受影响的数字范围,例如我的数据库中有101-200、301-900
,然后我想删除151-160
,因此它将选择使用受影响的行,即101-200
行,然后删除它并用新的行进行更改,这是我从上面的数组中获得的数据,这将解释old\u id
的用途:
"id" "item_id" "serial_id" "first" "last"
"3" "1" "1" "101" "150"
"4" "1" "1" "161" "200"
"5" "1" "1" "301" "350"
"7" "1" "1" "701" "800"
"9" "1" "1" "802" "810"
"10" "1" "1" "821" "900" ->the affected row
如果您想知道什么是函数insertData()
看起来像:
public function insertData($table,array $data){
$sql = "INSERT INTO $table SET ";
$vals = [];
foreach($data as $key => $val){
$vals[] = "$key = '$val'";
}
$sql .= implode(',',$vals);
return $this->db->run($sql);
}
这个解决方案对我很有效,我只是想问一下,如何在代码行更少的情况下或以更智能的方式获得相同的结果。您是在问一系列可接受的数字吗?因为那只是一个。想象一下,这是一张支票,它像一张有数字的纸,你从11-20中取了中间,你需要知道什么是剩下的页,或者你可以把纸从开始到中间或者中间,直到中间或者中间,当我得到这个时,你从一行开始[1-1000 ]。每次删除一个范围(例如100-200)时,都要将1-1000更新为201-1000,并添加一个新行1-99,对吗?