0

I have an dynamic array which looks like :

array(
  "animal > hairy > dog > Médor",
  "animal > hairy > dog > Fido",
  "animal > hairy > dog > Max",
  "animal > hairy > cat > Scratchy",
  "animal > hairy > bear > badBoy"
)

How can I obtain easily an array where the similar-prefixes are deleted ?
I mean, I want to obtain :

array(
  "dog > Médor",
  "dog > Fido",
  "dog > Max",
  "cat > Scratchy",
  "bear > badBoy"
)

Of course, there is a way comparing letter by letter, and value by value but it's an high complexity solution (Number of Similar Letter * Number of Value) and i guess it exists a better way to do that...

Any help ? Thanks,

user1978142
  • 7,946
  • 3
  • 17
  • 20
Dam Fa
  • 448
  • 4
  • 16
  • 1
    You can build a Trie ( http://en.wikipedia.org/wiki/Trie ) and then you walk down the Trie until you find a node with more than one child. – WizKid May 22 '14 at 00:35
  • Ahhhh so this is what it is called, I only call this data structure. – Bryan P May 22 '14 at 00:40
  • The guy said, **Of course, there is a way comparing letter by letter, and value by value but it's an high complexity solution** which means he is well aware of those. What he needs is an easier way to do it. – Bryan P May 22 '14 at 00:45
  • Building a Trie would be even slower. The post I linked to minimize number of compares. – WizKid May 22 '14 at 00:49
  • 1
    But if you never found an answer, you can check the link WizKid provided and check **Gumbo**'s answer which is the shortest and possibly the most efficient out of all the answers there – Bryan P May 22 '14 at 00:49

2 Answers2

0

Sorry, should've more explicit; editing. Use strrpos (reverse search on string) and substr 2 times recursively:

for ($i = 0; $i < $numRows; $i++) {
  $Result = substr($Array[$i],0,strrpos($Array[$i], '> ')-1);
  $Result = substr($Array[$i],strrpos($Result, '> ')+2);    
}

This will give you the initial position for the wanted string, which can then be easily obtained.

Bernardo Dal Corno
  • 1,858
  • 1
  • 22
  • 27
0

PHP has lots of built-in sting functions, preg_replace is probably the most powerful and the one I use by default. str_replace would also work. Try this.

<?php

$bigArray = array(
  "animal > hairy > dog > Médor",
  "animal > hairy > dog > Fido",
  "animal > hairy > dog > Max",
  "animal > hairy > cat > Scratchy",
  "animal > hairy > bear > badBoy"
);

$numRows = count($bigArray);

echo "The number of rows is $numRows<br />";

$newArray = array();
for ($i = 0; $i < $numRows; $i++) {

    $newRow = preg_replace('/animal > hairy > /', '', $bigArray[$i]);    
    array_push($newArray, $newRow);

}
for ($i = 0; $i < $numRows; $i++) {
    echo "$newArray[$i] <br />";
}

?>

This is the output

The number of rows is 5
dog > Médor 
dog > Fido 
dog > Max 
cat > Scratchy 
bear > badBoy 
JScarry
  • 1,507
  • 1
  • 13
  • 25
  • Sorry but the deal is that we don't know that "animal > hairy >" is the repetitive-prefix... Your answer doesn't answer to the question. Thanks anyway. – Dam Fa May 22 '14 at 22:41