| 
	
 | 
 Posted by Thomas Mlynarczyk on 11/17/07 17:13 
Hello, 
 
I have two arrays like this: 
 
$aSearch = array( 'A', 'B', 'C.D', 'E', 'F' ); 
$aSubject = array( 'A' => 0, 'A.B' => 1, 'X' => 2, 'C.D.E' => 3, 'A.B.C' =>  
4 ); 
 
Now I want to search $aSubject for the longest key that consists of the  
first x elements of $aSearch concatenated by "." (where x = 1...count(  
$aSearch ) ). In other words, he algorithm involved should check for the  
existence of the following keys in $aSubject: 
 
A 
A.B 
A.B.C.D 
A.B.C.D.E 
A.B.C.D.E.F 
 
and return the longest match - in the example 'A.B'. Now I could first  
create an array of all keys to search for and then loop through them to find  
a match in $aSubject, but I wonder if there is a more efficient, more  
elegant solution. There doesn't seem to be any PHP function that could help  
with this specific problem. 
 
Greetings, 
Thomas 
 
--  
C'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison! 
(Coluche)
 
  
Navigation:
[Reply to this message] 
 |