开发者

cleaning same values in array

开发者 https://www.devze.com 2023-04-13 01:04 出处:网络
I want to clear same values in array. For example so be my array: array(0=>\"1\",1=>\"1\",2=>\"3\",3=>\"1\",4=>\"6\");

I want to clear same values in array. For example so be my array:

array(0=>"1",1=>"1",2=>"3",3=>"1",4=>"6");

I wan开发者_开发技巧t ot get as:

array(0=>"1",1=>"3",2=>"6");

How?


<?php
$input = array(0=>"1",1=>"1",2=>"3",3=>"1",4=>"6");
$result = array_values(array_unique($input));
print_r($result);
?>

array_unique

Array
(
    [0] => 1
    [2] => 3
    [4] => 6
)

array_values with array_unique

Array
(
    [0] => 1
    [1] => 3
    [2] => 6
)


With a combination of array_unique [docs] and array_values [docs]:

$array = array_values(array_unique($array));


I believe you want the array_unique() function ( http://php.net/manual/en/function.array-unique.php ):

$arr = array_unique(array(0 => '1', 1 => '1', 2 => '2'));

will return:

array(0=> '1', 2 => '2')


I believe you can use array_slice for this purpose. Then edit the keys' values manually

Edit: To remove a key/value pair, call the unset() function on it. from here under Creating/modifying with square bracket syntax section.

0

精彩评论

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

关注公众号