如何检查数组是否有一些与另一个数组相同的元素,并从数组中弹出这些元素[复制]
This question already has an answer here:
- Deleting an element from an array in PHP 37 answers
I have an array
like this:
array(1) {
[0]=>
string(16) "1785328791698338"
}
And other array
like this:
array(7) {
[0]=>
string(17) "10207252567926988"
[1]=>
string(17) "10208823390691752"
[2]=>
string(17) "10209064245580796"
[3]=>
string(16) "1274474365912572"
[4]=>
string(16) "1294280923934896"
[5]=>
string(16) "1317727711586522"
[6]=>
string(16) "1785328791698338"
}
I should check if some of the elements(in this case only one, but it can vary) from first array are same as some elements in second array, and if they are, to remove them from first array. I tried doing it this way, but it does't work:
function findSameValuesOfArrays($arrayOne,$arrayTwo){
$newArray=array();
foreach($arrayOne as $a){
$newArray[0]=$a;
}
foreach($arrayTwo as $b){
$newArray[1]=$b;
}
if (strpos($newArray[1],$newArray[0])) {
return true;
}
}
This is just to find if there are same elements, and then i would probably unset key where those values are. But function returns NULL
.
</div>
此问题已经存在 这里有一个答案: p>
-
从PHP中删除数组中的元素
37 answers
li>
ul>
div>
我有一个
array code>,如下所示: p>
array(1){ [0] =&gt; string(16)“1785328791698338” } code> pre> \ n
和其他
array code>这样: p>
array(7){ [0] =&gt; string(17) “10207252567926988” [1] =&gt; string(17)“10208823390691752” [2] =&gt; string(17)“10209064245580796” [3] =&gt; string(16 )“1274474365912572” [4] =&gt; string(16)“1294280923934896” [5] =&gt; string(16)“1317727711586522” [6] =&gt; ; string(16)“1785328791698338” } code> pre>
我应该检查一些元素(在这种情况下只有一个,但它可以变化) 第一个数组与第二个数组中的某些元素相同,如果是,则从第一个数组中删除它们。 我尝试这样做,但它不起作用: p>
function findSameValuesOfArrays($ arrayOne,$ arrayTwo){ $ newArray = array(); foreach ($ arrayOne as $ a){ $ newArray [0] = $ a; } foreach($ arrayTwo as $ b){ $ newArray [1] = $ b; } if (strpos($ newArray [1],$ newArray [0])){ return true; } } code> pre>
这只是 找到是否有相同的元素,然后我可能会取消设置这些值的键。 但是函数返回
NULL code>。 p> div>
array_diff
does exactly what you want.
$sourceArr = array(1,2,3,4,5);
$filterArr = array(2,4);
$result = array_diff($sourceArr, $filterArr);
var_dump($result);
Result:
array(3) {
[0]=>
int(1)
[2]=>
int(3)
[4]=>
int(5)
}
You can use array_intersect
here like as:
$arr3 = array_intersect($arr1,$arr2);
print_r($arr3);
Here is an Examples:
Or You can read more at: http://php.net/manual/en/function.array-intersect.php
Try this:
$arrFirst = array("1785328791698338","10207252567926988");
$arrMain = array("10207252567926988","10208823390691752","10209064245580796","1274474365912572","1294280923934896","1317727711586522","1785328791698338");
foreach ($arrFirst as $key => $value) {
if(in_array($value, $arrMain )) { // check if value exist in seconf array
unset($arrFirst[$key]); // if yes - unset that value
}
}
You can use array_intersect here to returns an array containing all the values of array1 that are present in all the arguments. and then array_diff
$arrFirst = array(0=>"1785328791698338",1=>"1785328791698334",3=>"1785328791698336");
$arrMain = array(0=>"10207252567926988",1=>"10208823390691752",3=>"10209064245580796",4=>"1785328791698338");
$arrIntersect = array_intersect( $arrMain,$arrFirst);
$resultArray = array_diff($arrFirst, $arrIntersect);
print "<pre>";print_r($resultArray);