Get identical words from two arrays

Posted on 2009-07-06
Last Modified: 2012-05-07
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
Question by:Pedro Chagas
  • 2

Expert Comment

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

Open in new window


Accepted Solution

Mokona earned 250 total points
ID: 24790397
The first post will repeat every single repetition, every time it occurs.
This one only repeats once.  
$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);
foreach($array_one as  $arr1) 
	foreach($array_two as $arr2)
		if(preg_match("@$arr1@", $arr2)) {
			$string .= $arr1 . " ";
echo $string;

Open in new window


Author Comment

by:Pedro Chagas
ID: 24790543
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wordpress Only run code if on a certain page 11 35
if (is_singular not working 5 39
How do I remove "" from json_encode 5 22
Why is my implode function empty 8 16
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question