Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get identical words from two arrays

Posted on 2009-07-06
3
Medium Priority
?
293 Views
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
0
Comment
Question by:Pedro Chagas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Expert Comment

by:Mokona
ID: 24790310
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

0
 
LVL 1

Accepted Solution

by:
Mokona earned 750 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 . " ";
			break;
		}
echo $string;

Open in new window

0
 
LVL 3

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
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

715 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