array_diff(): Difference between two arrays
Returns all elements of $array1 which are not present in $array2 ($new_array=$array1 without $array2).
Example with two arrays
Output is here.
Keys of the first index are retained. ( no re-indexing done here)
Without any common element
Output is here
Keys of the first array is retained without any re-indexing.
associative array with literal keys
Output is here
For example let us use one array a1 and store text entered by user in a web form after converting the text string to an array. Now to prevent spammers posting URL or posting of some bad words we will develop another array a2 where all negative words we will store. These words we don't want to be present in the first ( a1) array.
Here is the example of how the array_diff works
The output of above code is here
0 -> first 2 -> third 4 -> fifth Array_diff() and array_diff_key()Using array_diff() comparison is done using the values, in case of array_diff_key() comparison is done using the keys only.How array_diff() is used to remove elements from file name array Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
| ||||
| ||||