Viteazul
Viteazul

Reputation: 181

Leading and trailing whitespaces are interfering with array_diff() comparisons

I have 2 arrays.
The first one is $teach_array and the second one is $langs_array.


Their respective values are:

$teach_array : Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
$langs_array : Array ( [0] => 2 [1] => 3 )

I'm trying to return a new array containing all the entries from $teach_array that are not present in $langs_array.

So the end result should be: Array ( [0] => 1 [3] => 4 [4] => 5 )

I have tried using a couple of methods including :

Option 1

$result = array_diff($teachArray, $language_1d_array);

This still returns all the values of $teach_array.

Option 2

 $result = array_diff_key($teachArray, $language_1d_array);

However, this only returns Array ( [2] => 3 [3] => 4 [4] => 5 ) which is not correct.

Option 3

$result = array_values(array_diff_key($teachArray, $language_1d_array));

This returns the same result as Option 2. I also tried using only array_diff instead of array_diff_key and it returns the same result as Option 1.


I did a var_dump on both of my arrays and here is the result.
$teach_array : array(5) { [0]=> string(5) " 1 " [1]=> string(5) " 2 " [2]=> string(5) " 3 " [3]=> string(5) " 4 " [4]=> string(5) " 5 " }
$lang_array : array(2) { [0]=> string(1) "2" [1]=> string(1) "3" }

Upvotes: 4

Views: 6759

Answers (4)

bnxuq
bnxuq

Reputation: 171

hope you have already found the solution, but just in case I want to point you on following.

Blockquote I did a var_dump on both of my arrays and here is the result. $teach_array : array(5) { [0]=> string(5) " 1 " [1]=> string(5) " 2 " [2]=> string(5) " 3 " [3]=> string(5) " 4 " [4]=> string(5) " 5 " } $lang_array : array(2) { [0]=> string(1) "2" [1]=> string(1) "3" }

No single value from $teach_array matches any value of $lang_array. Because there are differently formatted values, one array contains whitespaces before and after the value you want to match " 2 ".

var_dump($teach_array) => array(5) { [0]=> string(5) " 4 " ... } var_dump($lang_array) => array(5) { [0]=> string(1) "2" ... }

I guess you have some whitespaces included. Please try again with:

$diff = array_diff(array_map('trim', $teach_array), $lang_array);

Upvotes: 7

Pete
Pete

Reputation: 1309

This is (basically) what you say you have. It works for me:

<?php
  $fred = array(0=>1, 1=>2, 2=>3, 3=>4, 4=>5);
  $bert = array(0=>2, 1=>3);
  $res = array_diff($fred, $bert);
  print_r($res);

Upvotes: 1

clearshot66
clearshot66

Reputation: 2302

PHPTester just tested yours, works fine for me..?

$teachArray =[1,2,3,4,5];
$langsarray =[2,3];
$result = array_diff($teachArray,$langsarray);
print_r($result);

works and prints 1, 4, 5 for me.

BUT...here's a solution for what you're trying to acquire: the values in teacher array that are not in langs

$new_array = array();

foreach($teach_array as $item){    // Loop the teacher_array
  if(!in_array($item,$langs_array)){   // If the teach_array value doesn't exist in the lang_array, add the value
    $new_array[] = $item;
  }
}

Upvotes: 3

johnmccuk
johnmccuk

Reputation: 155

I'm sure theres a more elegant way, but this works:

$teach = [1, 2, 3,4, 5];
$langs = [2, 3];

$result = [];

foreach ($teach as $key => $t) {
    if (!in_array($t, $langs)) {
        $result[$key] = $t;
    }
}
var_dump($result);

Upvotes: 2

Related Questions