I have 2 arrays.
The first one is $teach_array and the second one is $langs_array.
$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 :
$result = array_diff($teachArray, $language_1d_array);
This still returns all the values of $teach_array.
$result = array_diff_key($teachArray, $language_1d_array);
However, this only returns Array ( [2] => 3 [3] => 4 [4] => 5 )
which is not correct.
$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" }
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);