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.

Recommended Answers

All 5 Replies

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

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/>";
}

?>
Member Avatar for diafol

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.