954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to matching 2 array keywords?

May i know how to matching 2 array keywords?

e.g I have
1st array keyword like kfc,mcd,fastfood,etc
2st array keyword like kfc,mcd

If i want to get the matching keywords among these 2 arrays, how to i write the matching code?

Thank advance for your help.

stanley87
Light Poster
43 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 
cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

Use foreach on the 2nd array and then compare the results in the foreach against the first array using in_array

simplypixie
Posting Pro in Training
447 posts since Oct 2010
Reputation Points: 116
Solved Threads: 82
 

Hi,

Here is the simple example for both of the responses above.

<?php
$first_ar = array('one','two','three','four','five','six','seven');
$second_ar = array('two','ten','eleven','five','twelve','nine','three');
	       $result = array_intersect($first_ar, $second_ar);
foreach($result as $something){
	
	
	echo "Matched: ".$something."<br/>";
}

?>
veedeoo
Posting Pro in Training
438 posts since Oct 2011
Reputation Points: 149
Solved Threads: 60
 

Yep like veedoo. However, be aware that this is case sensitive:

'One' and 'one' will not match.

If you want a case-insensitive match, you need to use something like array_map and choose a suitable function like strtolower or mb_strtolower (for UTF-8 compliance). DO this for each array and then do array_intersect.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,796 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Thanks guys for you all info,i have been done some simple matching way regarding your guys help.Anyway,thanks again for you guys.

stanley87
Light Poster
43 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: