Php 如何使用逗号删除相同的值并获得最终值?

Php 如何使用逗号删除相同的值并获得最终值?,php,Php,我要做的是在数组中获取相同的值,删除数组中的相同值,然后在删除数组中的相同值后获取整个数组 这是我的代码,但卡住了 $userid = "1087,1088,1089,1090,1091"; $user_explode = explode(",",$userid); $got_user = "no"; foreach($user_explode as $userid_row){ if($userid_row == 1088){ $got_user = "yes"; }

我要做的是在数组中获取相同的值,删除数组中的相同值,然后在删除数组中的相同值后获取整个数组

这是我的代码,但卡住了

$userid = "1087,1088,1089,1090,1091";
$user_explode = explode(",",$userid);
$got_user = "no";
foreach($user_explode as $userid_row){
    if($userid_row == 1088){
      $got_user = "yes";
    }
}
这就是我想要的

$id = '1,2,3,4,5';
if($id == 2){
 $result = '1,3,4,5';
}
echo $result;
你知道怎么解决我的问题吗? 感谢您使用删除不需要的值

$userid = "1087,1088,1089,1090,1091";
$user_explode = explode(",",$userid);
$got_user = "no";
foreach($user_explode as $key => $userid_row){
    if($userid_row == 1088){
      unset($user_explode[$key]);
      $got_user = "yes";
    }
}

echo(implode(',',$user_explode));

您可以分解为阵列,移除该项目并返回阵列

$userid = "1087,1088,1089,1090,1091";
$user_explode = explode(",",$userid);
$ids = array_flip($user_explode);
unset($ids['1088']);
$user_explode = array_flip($ids);
$final = implode(",", $user_explode);
您可以使用函数获取要搜索的值的键。如果找到了密钥,则将元素从数组中取消设置,最后将数组内爆并将其回显

尝试:

为什么不呢

你可以试试这个

$userid = array("1087","1088","1089","1090","1091");
foreach($userid as $key=>$id){
    if($id == 1088){
        unset($userid[$key]);
    }
}
print_r($userid);

谢谢兄弟,你救了我的命
<?php
    $id = "1,2,3,4,5";
    $got_id = false; // Not found by default
    $ids_array = array_map("intval", explode(",", $id)); // intval just to make sure these are correct IDs, I mean, transform them to integers
    $search = 2; // You're searching for id = 2

    foreach ($ids_array AS $key => $value)
    {
        if ($value == $search) // ID exists, remove it from the array and return the modified array
        {
            unset($ids_array[$key]);
            $result = implode(",", $ids_array);
            $got_id = true; // Found ID
            echo $result;
        }
    }
?>
<?php

$id = '1,2,3,4,5';
$result = implode(',', 
    array_filter(
        explode(',', $id),
        function($id) {
            if ($id != 2)
                return true;
        }
    )
);

echo $result;
1,3,4,5
$userid = array("1087","1088","1089","1090","1091");
foreach($userid as $key=>$id){
    if($id == 1088){
        unset($userid[$key]);
    }
}
print_r($userid);