2

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.

1
  • In simple terms you can say: You want to delete all elements from the second array in the first one. This means as explained in the duplicate $result = array_diff($yourFirstArray, $secondArray); Commented May 13, 2016 at 11:41

4 Answers 4

4

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)
}

https://3v4l.org/IvmHH

Sign up to request clarification or add additional context in comments.

Comments

0

You can use array_intersect here like as:

$arr3 = array_intersect($arr1,$arr2);
print_r($arr3);

Here is an Examples:

https://3v4l.org/kqPV2

https://3v4l.org/qZpgf

Or You can read more at: http://php.net/manual/en/function.array-intersect.php

Comments

0

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
   }
}

Comments

0

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.