Solved

Get identical words from two arrays

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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.  …
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 dynamically set the form action using jQuery.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now