php array_push() -> 如果数组已经包含值,如何不推送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10096019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php array_push() -> How not to push if the array already contains the value
提问by Marc
I am using the following loop to add items to an an array of mine called $liste. I would like to know if it is possible somehow not to add $value to the $liste array if the value is already in the array? Hope I am clear. Thank you in advance.
我正在使用以下循环将项目添加到名为 $liste 的数组中。我想知道如果值已经在数组中,是否有可能以某种方式不将 $value 添加到 $liste 数组中?希望我清楚。先感谢您。
$liste = array();
foreach($something as $value){
array_push($liste, $value);
}
回答by Rocket Hazmat
回答by Niet the Dark Absol
Two options really.
真的有两个选择。
Option 1: Check for each item and don't push if the item is there. Basically what you're asking for:
选项1:检查每个项目,如果项目在那里就不要推送。基本上你要的是什么:
foreach($something as $value) {
if( !in_array($value,$liste)) array_push($liste,$value);
}
Option 2: Add them anyway and strip duplicates after:
选项 2:无论如何添加它们并在之后删除重复项:
foreach($something as $value) {
array_push($liste,$value);
}
$liste = array_unique($liste);
By the look of it though, you may just be looking for $liste = array_unique($something);.
不过,从表面上看,您可能只是在寻找$liste = array_unique($something);.
回答by bjelli
maybe you want to use it as an associative array instead. it's implemented as (something like) a hash table, so you get constant insert time instead of linear.
也许您想将其用作关联数组。它被实现为(类似于) 哈希表,因此您可以获得恒定的插入时间而不是线性的。
function find_uniq( $something ) {
foreach($something as $value){
$liste[$value]++;
}
return array_keys( $liste );
}
回答by NoOorZ24
As this question is using not the best practice code to begin with I find all the answers here to be over-complicated.
由于这个问题不是使用最佳实践代码开始的,我发现这里的所有答案都过于复杂。
Solving code in question:
解决有问题的代码:
Basically what is tried to do in question itself is filtering out repetitions. Better approach:
基本上,自己试图做的是过滤掉重复。更好的方法:
$liste = array_unique($something);
If adding elements to an array that is not empty:
如果向非空数组添加元素:
$liste = array_unique(array_merge($liste, $something));
If you are using array_push():
如果您使用 array_push():
Unless you are actually using return value of array push you should really use:
除非您实际使用数组推送的返回值,否则您应该真正使用:
$liste[] = $value;
for shorter syntax and minor performance increase
更短的语法和轻微的性能提升
回答by Jazz
You can simply check this condition before calling array_push(). Use array_search()and use a strong comparison to falseto see if the value is present:
您可以在调用之前简单地检查此条件array_push()。使用array_search()并使用强比较来false查看值是否存在:
foreach( $something as $value ){
if( array_search( $value, $liste, true ) === false ){
array_push( $liste, $value );
}
}
(By the way: Add ,trueto array_searchto use "strict checking". This will use ===for comparisons instead of ==)
(顺便说一句:添加,true到array_search使用“严格检查”。这将===用于比较而不是==)
回答by doublejosh
Save logic and improve speed by keeping it logic-less. Just keep overwriting.
通过保持逻辑无逻辑来保存逻辑并提高速度。只是继续覆盖。
$list = array();
foreach ($something as $value) {
if (!is_object($value) && !is_array($value)) {
$list[$value] = $value
}
}
$list = array_values($list);
回答by Nick
The right answer is much simpler. Push everything, but then use array_unique()function:
正确答案要简单得多。推送所有内容,然后使用array_unique()功能:
array_push($array, $new_member);
$array = array_unique($array);
回答by Amir Fo
I have another solution for you! You can use keys as values And keys will never be duplicated.
我有另一个解决方案给你!您可以使用键作为值并且键永远不会重复。
$arr = ["A" => true, "B" => true, "C" => true];
$item = "A";
$arr[$item] = true;
Usage:
用法:
foreach($arr as $value => $helper){
echo $value;
}
Set class
设置班级
OK, Let me write a class for you! The arrays do not allow duplicated items are called sets.
好的,让我为你写一个类!数组不允许重复的项目称为集合。
class Set{
private $countainer;
public function get(){
return $this->container;
}
public function push($item){
$this->container[$item] = true;
}
public function delete($item){
unset($this->container[$item]);
}
}
Note: This will not work for associative arrays and values.
注意:这不适用于关联数组和值。
回答by ITWitch
Great answers are already present above, but if you have multiple array_push() all over your code, it would be a pain to write if(in_array()) statements every time.
上面已经给出了很好的答案,但是如果您的代码中有多个 array_push(),那么每次都编写 if(in_array()) 语句会很痛苦。
Here's a solution that will shorten your code for that case: Use a separate function.
这是一个可以缩短这种情况下代码的解决方案:使用单独的函数。
function arr_inserter($arr,$add){ //to prevent duplicate when inserting
if(!in_array($add,$arr))
array_push($arr,$add);
return $arr;
}
Then in all your array_push() needs, you can call that function above, like this:
然后在您所有的 array_push() 需求中,您可以调用上面的函数,如下所示:
$liste = array();
foreach($something as $value){
$liste = arr_inserter($liste, $value);
}
If $value is already present, $liste remains untouched.
如果 $value 已经存在,则 $liste 保持不变。
If $value is not yet present, it is added to $liste.
如果 $value 尚不存在,则将其添加到 $liste。
Hope that helps.
希望有帮助。

