独特的多维数组基于价值(而不是数组键) [英] Multidimensional array unique based on value (not array key)

查看:121
本文介绍了独特的多维数组基于价值(而不是数组键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多维数组,我需要与独特性进行排序,因为我有重复的记录,所以我需要 array_unique 来访问数组和值删除重复值,如:

 阵列

    [0] =>排列
        (
            [ID] => 324
            [TIME_START] => 1301612580
            [等级] => 0.002
            [input_level] => 0.002
        )    [1] =>排列
        (
            [ID] => 325
            [TIME_START] => 1301612580
            [等级] => 0.002
            [input_level] => 0.002
        )    [2] =>排列
        (
            [ID] => 326
            [TIME_START] => 1301612580
            [等级] => 0.002
            [input_level] => 0.002
        )

有重复 TIME_START ,他们都是一样的,也级别 input_level ,但他们都没有受到影响,只是如果有匹配 TIME_START 应该删除它,并处理整个阵列的(数组比你想象的大,但我只是贴数组的一个小例子)的。应该删除愚弄和返回是这样的:

 阵列

    [0] =>排列
        (
            [ID] => 324
            [TIME_START] => 1301612580
            [等级] => 0.002
            [input_level] => 0.002
        )

问题我发现,没有工作:


解决方案

  $输入=阵列(/ *数据* /);
$ TEMP =阵列();
$键=阵列();的foreach($输入为$关键=> $数据){
    未设置($数据['身份证']);
    如果(!in_array($数据,$温度)){
        $温度[] = $的数据;
        $键[$关键] =真;
    }
}$输出= array_intersect_key($投入,$键);

  $输入=阵列(/ *数据* /);
$ TEMP = $输入;的foreach($温度为&放大器; $数据){
    未设置($数据['身份证']);
}$输出= array_intersect_key($输入,array_unique($ TEMP));

I have a multidimensional array which I need to be sorted with uniqueness as I have duplicated records, so I need array_unique to go through the array and remove duplicates by the value, e.g.

Array
(
    [0] => Array
        (
            [id] => 324
            [time_start] => 1301612580
            [level] => 0.002
            [input_level] => 0.002
        )

    [1] => Array
        (
            [id] => 325
            [time_start] => 1301612580
            [level] => 0.002
            [input_level] => 0.002
        )

    [2] => Array
        (
            [id] => 326
            [time_start] => 1301612580
            [level] => 0.002
            [input_level] => 0.002
        )
)

There are duplicated time_start, which they are all the same, also level and input_level but they are not to be affected, only if there are matching time_start it should remove it and process the whole array (the array is bigger than you think, but I just posted a small example of the array). Should remove dupes and return like this:

Array
(
    [0] => Array
        (
            [id] => 324
            [time_start] => 1301612580
            [level] => 0.002
            [input_level] => 0.002
        )
)

Questions I've found that didn't work:

解决方案

$input = array( /* your data */ );
$temp  = array();
$keys  = array();

foreach ( $input as $key => $data ) {
    unset($data['id']);
    if ( !in_array($data, $temp) ) {
        $temp[]     = $data;
        $keys[$key] = true;
    }
}

$output = array_intersect_key($input, $keys);

or

$input = array( /* your data */ );
$temp  = $input;

foreach ( $temp as &$data ) {
    unset($data['id']);
}

$output = array_intersect_key($input, array_unique($temp));

这篇关于独特的多维数组基于价值(而不是数组键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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