开发者

unique array values

开发者 https://www.devze.com 2023-04-02 00:29 出处:网络
I have string of comma separated values. 1,2,3,4,4,4,4,4,4,4,4,01633,4,4 I need to remove the duplicates, so I though of using

I have string of comma separated values.

1,2,3,4,4,4,4,4,4,4,4,01633,4,4

I need to remove the duplicates, so I though of using

array_unique($str);

However, it returns no results. So I decided to output it to see what I have:

print_r($str);
// returns:开发者_如何转开发 1,2,3,4,4,4,4,4,4,4,4,01633,4,4

I'm a little lost. I checked if it is an array and I got true. Here's how that string is created:

$str = '1,2,3';

foreach ($a as $b) {
    $str.= ','.$b;
}

What am I missing here?


$str = explode(',', $str); // create array
$newArray = array_unique($str); // then process

actually, though... just do your array_unique() on $a before the string is created.

$a = array_unique($a);

then...

foreach ($a as $b) { // and so on


Convert to an array, remove the repeat values, convert to string

$str = 'whatever';
$arr = explode( ',', $str );
$newArr = array_unique( $arr );
$newStr = implode( ',', $newArr );


Explode on comma, make unique, glue pieces back together:

$str = implode(',', array_unique(explode(',', $str)));
0

精彩评论

暂无评论...
验证码 换一张
取 消