通过特定值从多维数组中删除重复项 [英] Remove Duplication from an multidimensional array by specific values

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

问题描述

我有一个数组

Array
(
    [0] => Array
        (
            [userTag] => All
            [fbId] => 10210118553469338
            [fName] => Danish
            [lName] => Aftab
            [imageUrl] => https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/22491765_10210410024475931_8589925114603818114_n.jpg?oh=7fa6266e7948ef2d218076857972f7e0
            [subsType] => gold
            [user_visible] => 0
        [distance] => 0.01
        [advising] => 0
        [avgRate] => 4
        [totalReview] => 2
        [us_seeker_type] => new
        [price] => 70
    )

[1] => Array
    (
        [userTag] => All
        [fbId] => 10210118553469338
        [fName] => Danish
        [lName] => Aftab
        [imageUrl] => https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/22491765_10210410024475931_8589925114603818114_n.jpg?oh=7fa6266e7948ef2d218076857972f7e0
        [subsType] => gold
        [user_visible] => 0
        [advising] => 0
        [distance] => 0.01
        [avgRate] => 4
        [totalReview] => 2
        [up_provider_type] => new
        [price] => 14
    )

[2] => Array
    (
        [userTag] => All
        [fbId] => 10210118553469338
        [fName] => Danish
        [lName] => Aftab
        [imageUrl] => https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/22491765_10210410024475931_8589925114603818114_n.jpg?oh=7fa6266e7948ef2d218076857972f7e0
        [subsType] => gold
        [user_visible] => 0
        [advising] => 0
        [distance] => 0.01
        [avgRate] => 4
        [totalReview] => 2
        [utr_trader_type] => new
        [price] => 
    )

[3] => Array
    (
        [userTag] => Seeker
        [fbId] => 10207897577195936
        [fName] => Saq
        [lName] => Khan
        [imageUrl] => https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/21151741_10207631130774942_8962953748374982841_n.jpg?oh=f5e5b9dff52b1ba90ca47ade3d703b01
        [subsType] => gold
        [user_visible] => 0
        [background] => 
        [topic] => 
        [distance] => 0.01
        [advising] => 0
        [avgRate] => 0
        [totalReview] => 0
        [us_seeker_type] => new
        [price] => 65
    )

[6] => Array
    (
        [userTag] => Seeker
        [fbId] => 709288842611719
        [fName] => Muhammad
        [lName] => Hasan
        [imageUrl] => https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/20264704_681395725401031_2098768310549259034_n.jpg?oh=36db5b6ed60214088750794d4e3aa3e6
        [subsType] => gold
        [user_visible] => 0
        [distance] => 0.02
        [advising] => 0
        [avgRate] => 0
        [totalReview] => 0
        [us_seeker_type] => new
        [price] => 75
    )
[17] => Array
    (
        [userTag] => Trader
        [fbId] => 2145752308783752
        [fName] => Jawaid
        [lName] => Ahmed
        [imageUrl] => https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/20992899_2068273703198280_4249128502952085310_n.jpg?oh=6df0be6ced405dd66ee50de238156183
        [subsType] => basic
        [user_visible] => 0
        [advising] => 0
        [distance] => 0
        [avgRate] => 0
        [totalReview] => 0
        [utr_trader_type] => new
        [price] => 
    )    
)

我只想删除(userTag = All)的重复项.

I only wanna Remove Duplicates of (userTag = All).

我不想删除(userTag = Seeker)或任何其他用户标签的重复项.

I don't wanna remove duplicates of (userTag= Seeker ) Or any other usertags.

我总共有10个用户标签,但我只想从userTag中删除重复的标签.

I have total 10 usertags but I wanna only remove duplicates from userTag.

我将以json对象的形式返回数据.

I will return data as a json object.

我正在研究Slim Framework.

I am working on Slim Framework.

我的代码如下.

$input = my-array
$dat =array();
$dat2 = array();
 foreach($input as $key => $value)
{
    $i=0;
    $j=0;
    if(count($dat)==0) 
    {
        $dat = $input[$key];

        $i++;
    }
    else {          
        if($input[$key]['userTag'] =="All"){
        if($this->check($input[$key]['fbId'], $dat)==false)

        {
              $dat[$i] = $input[$key];

            $i++;
        }
        }
        else  {
          $dat2[$j] = $input[$key];
          $j++;
        }
    }
}
     $data=  array_merge($dat,$dat2);
      return $data;

检查功能在这里

public function check($val, $array) {
                    foreach ( $array as $vl) {
                    if ($val == $array[$vl]['fbId']) {
                    return true;
                    break;
                }
            }
            return false;
        }

推荐答案

您可以使用它,这将从数组中删除所有重复的值.

You can use this, This will remove all duplicate values from your array.

$arr = array_map("unserialize", array_unique(array_map("serialize", $arr)));

更新后的答案

$temp = array();
foreach($arr as $key => $val) {
    if ($val['userTag'] == "All" && empty($temp)) {
        $temp[$key] = $arr[$key];
        unset($arr[$key]);
    }
    else if ($val['userTag'] == "All") {
        unset($arr[$key]);
    }
}
$arr = array_merge($arr, $temp);

这篇关于通过特定值从多维数组中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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