2 u012575301 u012575301 于 2014.12.06 21:01 提问

怎样删除二维数组中相同的一位数组 并保持相同键名 求大神写个函数

Array
(
[0] => Array
(
[year] => 2013-2014
[term] => 1
[course_code] => 00008069
[course_name] => 咖啡世界
[course_nature] => 任意选修
[course_attribution] => 人文素养类
[credit] => 2.0
[point] => 4.1
[grade] => 91
[minor_tag] => 0
[makeup_grade] =>

[rebuild_grade] =>

[begin_college] => 旅游文化产业学院
[remark] =>

[rebuild_tag] => 0
[0] => 77
[1] => 2
)

                    [1] => Array
                            (
                                    [year] => 2013-2014
                                    [term] => 1
                                    [course_code] => 00008069
                                    [course_name] => 咖啡世界
                                    [course_nature] => 任意选修
                                    [course_attribution] => 人文素养类
                                    [credit] => 2.0
                                    [point] => 4.1
                                    [grade] => 91
                                    [minor_tag] => 0
                                    [makeup_grade] =>  
                                    [rebuild_grade] =>  
                                    [begin_college] => 旅游文化产业学院
                                    [remark] =>  
                                    [rebuild_tag] => 0
                                    [0] => 77
                                    [1] => 2
                            )

                    [2] => Array
                            (
                                    [year] => 2013-2014
                                    [term] => 1
                                    [course_code] => 00008069
                                    [course_name] => 咖啡世界
                                    [course_nature] => 任意选修
                                    [course_attribution] => 人文素养类
                                    [credit] => 2.0
                                    [point] => 4.1
                                    [grade] => 91
                                    [minor_tag] => 0
                                    [makeup_grade] =>  
                                    [rebuild_grade] =>  
                                    [begin_college] => 旅游文化产业学院
                                    [remark] =>  
                                    [rebuild_tag] => 0
                                    [0] => 77
                                    [1] => 2
                            )

)

如上面 怎样让它变成
Array
(
[0] => Array
(
[year] => 2013-2014
[term] => 1
[course_code] => 00008069
[course_name] => 咖啡世界
[course_nature] => 任意选修
[course_attribution] => 人文素养类
[credit] => 2.0
[point] => 4.1
[grade] => 91
[minor_tag] => 0
[makeup_grade] =>

[rebuild_grade] =>

[begin_college] => 旅游文化产业学院
[remark] =>

[rebuild_tag] => 0
[0] => 77
[1] => 2
)
}

2个回答

piaoxue0145
piaoxue0145   2014.12.08 11:34

引用自http://www.nowamagic.net/librarys/veda/detail/1371
01
//二维数组去掉重复值
02
function array_unique_fb($array2D)
03
{
04
foreach ($array2D as $v)
05
{
06
$v = join(",",$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
07
$temp[] = $v;
08
}
09

10
$temp = array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
11
foreach ($temp as $k => $v)
12
{
13
$temp[$k] = explode(",",$v); //再将拆开的数组重新组装
14
}
15
return $temp;
16
}
如果你想保留数组的键值,可以用下面的函数:

01
//二维数组去掉重复值 并保留键值
02
function array_unique_fb($array2D)
03
{
04
foreach ($array2D as $k=>$v)
05
{
06
$v = join(",",$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
07
$temp[$k] = $v;
08
}
09
$temp = array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
10
foreach ($temp as $k => $v)
11
{
12
$array=explode(",",$v); //再将拆开的数组重新组装
13
$temp2[$k]["id"] =$array[0];

14
$temp2[$k]["litpic"] =$array[1];
15
$temp2[$k]["title"] =$array[2];
16
$temp2[$k]["address"] =$array[3];
17
$temp2[$k]["starttime"] =$array[4];
18
$temp2[$k]["endtime"] =$array[5];
19
$temp2[$k]["classid"] =$array[6];
20
$temp2[$k]["ename"] =$array[7];
21
}
22
return $temp2;
23
}
大概就这样啦。

asasaas121
asasaas121   2014.12.09 17:31
 $arr=array(
        0=>array(
        'name'=>"xxx",
        'age'=>23,
        'school'=>"yyyy大学"
),
        1=>array(
        'name'=>"xxx",
        'age'=>23,
        'school'=>"yyyy大学"
),
        2=>array(
                'name'=>"xxx",
                'age'=>23,
                'school'=>"yyyy大学"
        )
        );
$delete_key=0;//需要找的重复的数组的下标值
$value=$arr[$delete_key];//获取其值
$key=array_keys($arr,$value);//需要同样的值的下标
foreach ($key as $k=>$v)
{
    if($v!=$delete_key)//第一个值不删,其它的删除
        unset($arr[$v]);
}
echo"<pre>";
var_dump($arr);
echo"</pre>";
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!