Link to home
Start Free TrialLog in
Avatar of Pedro Chagas
Pedro ChagasFlag for Portugal

asked on

replace similar words in array

Hi, I have a array that contain words, and some words are similar.
So, if in array I have values like shoe or shoes, I want replace shoes for shoe:
I have this array:
Array
(
    [0] => shoes
    [1] => glove
    [2] => gloves
    [3] => ties
    [4] => bracelets
    [5] => gloves
    [6] => shoe
    [7] => shoes
)
and I want:

Array
(
    [0] => shoes
    [1] => glove
    [2] => glove
    [3] => ties
    [4] => bracelets
    [5] => glove
    [6] => shoe
    [7] => shoe
)

First the script have to detect if have words like shoe and shoes (similar words), and if have,  the word shoes are replace by shoe (is just detect the 's').

How I do this?

Regards,
JC
Avatar of atannus
atannus

You need to perform two steps:

1. Tell PHP what (shoes) should be replaced by what (shoe).

One interesting way of doing this is by creating an instruction array, like so:

array (
[shoe ]=> shoes
[shirt] => shirts
[pant] => pants
)

Then, you loop into this array, and for every $key => $value pair, you search the target array for $value, replacing it with $key.

It is even possible that every key in the instruction array holds another array, with ALL the words that sould be replaced with shoe (shoes, shoo, shows).

Does this help?
Avatar of Pedro Chagas

ASKER

Yes this help, and I understand your idea. The problem is I don't know work with arrays. If I know working with arrays, maybe I get the solution in this time.
Can be more precise?

Regards,
JC
Why is the first word in the array "shoes" not replaced by "shoe" in the result array?
Hi, I´m sorry, the correct is:
Array
(
    [0] => shoe //change
    [1] => glove
    [2] => glove
    [3] => ties
    [4] => bracelets
    [5] => glove
    [6] => shoe
    [7] => shoe
)
If I have the array $words that contain this values:
Array
(
    [0] => shoes
    [1] => glove
    [2] => gloves
    [3] => ties
    [4] => bracelets
    [5] => gloves
    [6] => shoe
    [7] => shoes
)

How I do for get this solution:
(
    [0] => shoe //change
    [1] => glove
    [2] => glove
    [3] => ties
    [4] => bracelets
    [5] => glove
    [6] => shoe
    [7] => shoe
)

Regards, JC
ASKER CERTIFIED SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, I try to test the code of 'cxr' but I get this warning:
Warning: Invalid argument supplied for foreach() in /home/tags/public_html/bastidores/teste.php on line 15

Warning: Invalid argument supplied for foreach() in /home/tags/public_html/bastidores/teste.php on line 21

The only changes I do in the code of 'cxr' is create teh array word!

Please view what happening with the code.

Regards, jc
<?
$palavras = "shoe shoes candle gloves glove glove";
$word = explode(' ', $palavras);
print_r($word);
# 1) collect all unique words
$all_words = array();
foreach($input as  $word) { //line15
  if(in_array($word,$all_words)) continue;
  $all_words[] = $word;
}
# 2) determine which words are plural version of existing singular word
$result = array();
foreach($input as  $word) { //line21
  if((substr($word,strlen($word)-1,1)=='s') # ends with 's'
    and in_array(substr($word,0,strlen($word)-1),$all_words)) # exists as singular
    $result[] = substr($word,0,strlen($word)-1);  # change to singular form
  else 
    $result[] = $word;
}
print_r($result);
?>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial