Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php在多维数组中删除和插入重复元素的计数_Php_Arrays_Multidimensional Array - Fatal编程技术网

使用php在多维数组中删除和插入重复元素的计数

使用php在多维数组中删除和插入重复元素的计数,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一套多维数组的借书 排列 ( [0]=>阵列 ( [投标]=>1 [begindate]=>2015-02-03 [标题]=>编程 ) ) 我已经有了删除副本的代码 0: begindate: "2015-02-03", bid: "1",title: "programming" 3: begindate: "2015-02-03", bid: "4", title: "english" 4: begindate: "2015-02-04"bid: "4"title: "en

我有一套多维数组的借书

排列 ( [0]=>阵列 ( [投标]=>1 [begindate]=>2015-02-03 [标题]=>编程 )

)

我已经有了删除副本的代码

0: begindate: "2015-02-03", bid: "1",title: "programming"    
3: begindate: "2015-02-03", bid: "4", title: "english"    
4: begindate: "2015-02-04"bid: "4"title: "english"
我想要的是在数组中插入重复的计数,这样我就可以得到这样的结果

0: begindate: "2015-02-03", bid: "1",title: "programming", count:"3"    
3: begindate: "2015-02-03", bid: "4", title: "english", count:"1"    
4: begindate: "2015-02-04", bid: "4", title: "english", count:"2"

这并不漂亮,但这会满足你的要求:

//main array here is $books
$clean = array();
foreach($books as $k1=>$v1){
    $exists = false;
    //does an entry already exist in $clean?
    foreach($clean as $k2=>$v2){
        if($v1["begindate"]==$v2["begindate"]&&$v1["bid"]==$v2["bid"]&&$v1["title"]==$v2["title"]){
            $exists = $k2;
            break;
        }
    }
    if($exists===false){
        //begin count, insert
        $v1["count"] = 1;
        array_push($clean,$v1);
    }else{
        //increment count
        $clean[$exists]["count"]++;
    }
}
print_r($clean);

只需使用一个容器来保存计数,每次重复出现时递增重复数据消除代码是什么?如果你只是简单地使用一个索引数组,那么很容易:每次遇到重复的数组时,只需增加
count
。粘贴你的代码即可删除重复的数组。我只需这样做代码sir$data=array\u map(“unserialize”,array\u unique(array\u map(“serialize”),array\u unique(array\u map(“serialize”),$data));副本应以起始日期和投标日期为基础。对不起,我只是一个处理多维数组的初学者
//main array here is $books
$clean = array();
foreach($books as $k1=>$v1){
    $exists = false;
    //does an entry already exist in $clean?
    foreach($clean as $k2=>$v2){
        if($v1["begindate"]==$v2["begindate"]&&$v1["bid"]==$v2["bid"]&&$v1["title"]==$v2["title"]){
            $exists = $k2;
            break;
        }
    }
    if($exists===false){
        //begin count, insert
        $v1["count"] = 1;
        array_push($clean,$v1);
    }else{
        //increment count
        $clean[$exists]["count"]++;
    }
}
print_r($clean);