I am attempting to use array_diff to exclude certain elements from an array. According to the docs: "Returns an array containing all the entries from array1 that are not present in any of the other arrays."
In my code below I have an array which represents the data return from the database when requesting a user. I have another array which contains the elements I don't want to be included in the array. Here is the code ...
$user = array(
'id' => '9',
'password' => 'CRYPT_BLOWFISH HASH',
'username' => 'Billy',
'phone' => '+447777777777');
$columnsToExclude = array('password', 'phone');
var_dump(array_diff($user, $columnsToExclude));
I'm not understanding what I'm doing wrong, unless I can't use a sequential array as the second argument.
What am I doing wrong?
Edit:
As suggested in comments, I have tried array_diff_key and I get the same
results.
array_diff_key($user, $columnsToExclude));
array_diffis working on values and not on keys.array_diff_keyphp.net/manual/en/function.array-diff-key.php