删除重复项,并设置有多少来自多维数组 [英] Remove duplicates and set how many there are from multi-dimensional array

查看:177
本文介绍了删除重复项,并设置有多少来自多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的StackOverflow,我需要一些帮助。

I'm new to stackoverflow and I need some help.

我想从PHP中的多维数组,如删除重复的:

I'm trying to remove the duplicates from a multi-dimension array in PHP such as:

阵列(

[0] =>阵列([开发平台] =>你好[数据] => 2015年1月1日[期限] => PHP
  [全] => 1)

[0] => Array ( [Plat] => hello [Data] => 01/01/2015 [Term] => PHP [Quan] => 1 )

[1] =>阵列([开发平台] =>你好[数据] => 2015年1月1日[期限] => PHP
  [全] => 1)

[1] => Array ( [Plat] => hello [Data] => 01/01/2015 [Term] => PHP [Quan] => 1 )

[2] =>阵列([开发平台] =>你好[数据] => 2015年3月1日[期限] => PHP
  [全] => 1)

[2] => Array ( [Plat] => hello [Data] => 03/01/2015 [Term] => PHP [Quan] => 1 )

[3] =>阵列([开发平台] =>你好[数据] => 2015年3月1日[期限] => PHP
  [全] => 1)

[3] => Array ( [Plat] => hello [Data] => 03/01/2015 [Term] => PHP [Quan] => 1 )

[4] =>阵列([开发平台] =>你好[数据] => 2015年3月1日[期限] => PHP
  [定量] => 1)

[4] => Array ( [Plat] => hello [Data] => 03/01/2015 [Term] => PHP [Quant] => 1 )

[5] =>阵列([开发平台] =>你好[数据] => 2015年3月1日[期限] => PHP
  [全] => 1)

[5] => Array ( [Plat] => hello [Data] => 03/01/2015 [Term] => PHP [Quan] => 1 )

和创建一个阵列,消除重复的并且发现这样会增加[拳]重复它的数量(通过数据过滤):

and create a array that removes the duplicates and adds to [Quan] the number of duplicates it as found like this (filtered by data) :

阵列(

[0] =>阵列([开发平台] =>你好[数据] => 2015年1月1日[期限] => PHP
  [全] => 2)

[0] => Array ( [Plat] => hello [Data] => 01/01/2015 [Term] => PHP [Quan] => 2 )

[1] =>阵列([开发平台] =>你好[数据] => 2015年3月1日[期限] => PHP
  [全] => 4)

[1] => Array ( [Plat] => hello [Data] => 03/01/2015 [Term] => PHP [Quan] => 4 )

我的code是如下因素:
$顶部是阵列。

My code is the folowing: $top is the array.

foreach($top as $value){
        if(!empty($temp_top)){
            for($i =0;$i<sizeof($temp_top);$i++){
                if($value['Data'] == $temp_top[$i]['Data'] ){
                    $temp_top[$i]['Quan'] +=1;
                }else{
                    $temp_top[] = $value;
                }
            }
        }else{
            $temp_top[] = $value;
        }

    }

我已经tryed一些答案,我发现这里的堆栈,如:

i've tryed some answers that I found here on stack such as:

$input = array_map("unserialize", array_unique(array_map("serialize", $top)));

但我不能添加多少有一些为[全] ..

but I can't add how many there are to [Quan]..

推荐答案

我找到了一个解决方案,我与其他职位的帮助。

I found a solution myself with the help of other posts.

下面是我的code的样本:

Here is a sample of my code:

$temp_top = array_map("unserialize", array_unique(array_map("serialize", $top)));
    $numvezesfound=0;

    foreach($temp_top as $key => $value){
            for($i =0;$i<sizeof($top);$i++){
                if($value['Data'] == $top[$i]['Data'] ){
                    $numvezesfound +=1;
                }
            }
            $temp_top[$key]['Quan'] = $numvezesfound;
            $numvezesfound = 0;
        }       
    $top = $temp_top;

如果有人能帮助我使这个code更美丽,我会在你的债务:)

if someone can help me make this code more beautifull I would be in your debt :)

感谢大家寿!

这篇关于删除重复项,并设置有多少来自多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆