SQL PHP HTML ASP JavaScript articles and free scripts to download

Difference between two arrays

We can find out difference of two arrays by using array_diff function. This function takes two arrays as input and then returns the result array. Syntax of this function is here.

$new_array = array_diff ($array1, $array2);

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

$a1=array("first", "second", "third","fourth","fifth");
$a2=array("second","fourth","sixth");

$new_array=array_diff($a1,$a2);

while (list ($key, $val) = each ($new_array)) {
echo "$key -> $val <br>";
}



The output of above code will be

0 -> first
2 -> third
4 -> fifth

Google+
MAZHAR HUSSAIN08-08-2010
PLEASE SEND ME SIR!DIFFERENCE BETWEEN FUNCTION AND ARRAY?
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer