Link to home
Start Free TrialLog in
Avatar of Pedro Chagas
Pedro ChagasFlag for Portugal

asked on

Get identical words from two arrays

Hi E's, I have two arrays, and these arrays contain words. $array_one contain a single words and $array_two contain more then one word, like:
$array_one ---- Array ( [0] => one [1] => two [2] => five [3] => ten );
$array_two ---- Array ( [0] => one four [1] => one five numbers [2] => seven eleven [3] => ten ten world space);

What I want is a string, that in this case return (the repeat words between arrays divided by space):
$string = "one five ten";

Regards, JC
Avatar of Mokona
Mokona

There you go
$string="";
foreach($array_one as  $arr1) 
	foreach($array_two as $arr2)
		if(preg_match("@" . $arr1 . "@", $arr2))
			$string .= $arr1 . " ";
 
echo $string;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mokona
Mokona

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pedro Chagas

ASKER

Hi @mokone, after echo $string; I put unset($string);. This is because I will use this code inside a while, and this new line is for $string not contain the last result.

Regards, JC