本文介绍了php array_push()->如果数组已经包含值,如何不推送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下循环将项目添加到名为$ liste的我的数组中.我想知道是否有可能以某种方式不将$ value添加到$ liste数组中(如果该值已在数组中)?希望我清楚.预先谢谢你.
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 = array();
foreach($something as $value){
array_push($liste, $value);
}
推荐答案
使用 in_array
,然后再推送.
You check if it's there, using in_array
, before pushing.
foreach($something as $value){
if(!in_array($value, $liste, true)){
array_push($liste, $value);
}
}
,true
启用严格检查".这样会使用===
而不是==
来比较元素.
The ,true
enables "strict checking". This compares elements using ===
instead of ==
.
这篇关于php array_push()->如果数组已经包含值,如何不推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!