Home >Backend Development >PHP Tutorial > 找出重复数据来,函数改写,该怎么解决

找出重复数据来,函数改写,该怎么解决

WBOY
WBOYOriginal
2016-06-13 13:38:41861browse

找出重复数据来,函数改写
数据库里这样子,
id wc we
1 q 3
2 a 0
3 q 3
4 b 1
5 b 1
6 q 2
7 q 2
8 g 3
我要得到:变成
id wc we
1 q 3-1/2
3 q 3-2/2
4 b 1-1/2
5 b 1-2/2
6 q 2-1/2
7 q 2-2/2

------解决方案--------------------
蛋痛了数个小时终于写好了,我擦!这个时候十分想念怪署黍啊...感觉代码写得太恶心了。
得到结果 循环数组拼装成 SQL update 数据库。搞成函数什么的就自己劳动啦,代码看不懂也莫问我,咔咔,太费脑子了.

Array
(
[0] => 1 23 234
[1] => 2 24 235-1/2
[2] => 3 24 235-2/2
[3] => 4 25 235-1/2
[4] => 5 25 235-2/2
[5] => 6 37 456-1/2
[6] => 7 37 456-2/2
)

PHP code

$array = array('1 23 234', '2 24 235', '3 24 235', '4 25 235', '5 25 235', '6 37 456', '7 37 456');
$newArray = array();
foreach ($array as $value) {
    preg_match('/^[0-9]+\s([0-9]+\s[0-9]+$)/', $value, $mArray);
    $newArray[] = $mArray[1];
}
$countArray = array_count_values($newArray);
$fixArray = array();

$i = 1;
foreach ($newArray as $newkey => &$value) {
    $newArrayKey = array();
    if (array_key_exists($value, array_flip($newArray))) {
        $newArrayKey = array_keys($newArray, $value);
        if (count($newArrayKey) !== 1) {
            $tmp = array_search($newkey, $newArrayKey);
            $tmp = intval($tmp) + 1;
            $fixArray[] = $i . ' ' . $value . '-' . $tmp . '/' . $countArray[$value];
        } else {
            $fixArray[] = $i . ' ' . $value;
        }
        $i++;
    }

}
print_r($fixArray);
<br><font color="#e78608">------解决方案--------------------</font><br>
妞妞,哪有事情做到一半的到底。起码也要注释解释一下吧。。<br><br>话说大版已好久没现身了。挺想念的。。 <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn