Solved

Get identical words from two arrays

Posted on 2009-07-06
3
287 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
  • 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 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 . " ";
			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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

810 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