简体   繁体   English

PHP - array_unique 不改变输出

[英]PHP - array_unique not changing output

I have an array $tmp:我有一个数组 $tmp:

$a = array(0 => 49, 1 => 49, 2 => 49);

after using array_unique($tmp) I'm getting this output:使用array_unique($tmp)我得到这个输出:

Array
(
    [0] => 49
    [1] => 49
    [2] => 49
)

and I want to get我想得到

Array
(
    [0] => 49
)

What am I doing wrong?我究竟做错了什么? Im new in PHP我是 PHP 新手

You don't only need to call that function you need to use the returned value as well.您不仅需要调用该函数,还需要使用返回值。 Do

$tmp=array_unique($tmp);

Just calling that function and not picking up the returned value does no good.只是调用该函数而不获取返回值是没有好处的。

There are some functions that operate on the original variable and hence you dont need to pick up their ret val for example sort() but array_unique() is not one of them.有一些函数对原始变量进行操作,因此您不需要获取它们的 ret val 例如sort()array_unique()不是其中之一。 Always refer to http://www.php.net/functionName to find out总是参考http://www.php.net/functionName来了解

$input = array(49,49,49);

$result = array_unique($input);

print_r($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM