通过特定值从多维数组中删除重复项

薇姬·吉尔

我有一个数组

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)的重复项。

我不想删除(userTag = Seeker)或任何其他usertag的重复项。

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

我将返回数据作为json对象。

我正在研究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;
        }

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

$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);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章