You are here: Re: Find longest matching key « PHP Programming Language « IT news, forums, messages
Re: Find longest matching key

Posted by Csaba Gabor on 11/17/07 23:10

On Nov 17, 6:13 pm, "Thomas Mlynarczyk" <tho...@mlynarczyk-
webdesign.de> wrote:
> $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 "."
....
> and return the longest match - in the example 'A.B'. Now I could first
....


function longestMatchingKey ($aSrch, $aSubj) {
if (array_key_exists($k=implode(".", $aSrch), $aSubj)) return $k;
for ($i=sizeof($aSrch);$k=substr($k,0,-strlen($aSrch[--$i])-1);)
if (array_key_exists($k, $aSubj)) break;
return $k; }


(5 lines total, one loop, no reversing, watch for wrapping) Though
perhaps not overly different from what you suggested...
Csaba Gabor from Vienna

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация