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
Solved

Get identical words from two arrays

Posted on 2009-07-06
3
288 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to use Initialization Vector for openssl_encrypt() 5 58
Email called spam 5 36
PHP Echo with auto submit 8 28
selector:validator cookies 4 30
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.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

840 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