从php中的多维数组中获取最大长度匹配字符串

时间:2014-04-10 08:20:45

标签: php arrays multidimensional-array

我有多维数组,我有另一个数组

代码:

SINGLE ARRAY作为A

Array
(
    [pattern] => 91919089
)

MULTIDIMENSIONAL ARRAY称为b

[1] => Array
    (
        [0] => 42
        [1] => VIETNAM
        [2] => 84
    )

[2] => Array
    (
        [0] => 46
        [1] => 3253
        [2] => 235
    )

[3] => Array
    (
        [0] => 48
        [1] => india
        [2] => 91
    )

[4] => Array
    (
        [0] => 49
        [1] => india
        [2] => 919
    )

[5] => Array
    (
        [0] => 50
        [1] => india
        [2] => 9191
    )

多维数组称为c

[1] => Array
    (
        [0] => 42
        [1] => VIETNAM
        [2] => 84
    )

[2] => Array
    (
        [0] => 46
        [1] => 3253
        [2] => 235
    )

[3] => Array
    (
        [0] => 48
        [1] => india
        [2] => 89
    )

[4] => Array
    (
        [0] => 49
        [1] => india
        [2] => 089
    )

[5] => Array
    (
        [0] => 50
        [1] => india
        [2] => 9089
    )

这里我们可以看到模式91919089与数组b的第3个第4和第5个元素匹配,并且每个的2个键分别是91,919和9191和5 [2]我想要9191. = 9191这是最大匹配字符串的结果模式91919089,结果是5

假设我将从多维数组中删除第5个元素,那么必须给出第4个与919匹配的结果。如果找不到匹配则需要从结束的另一个数组c匹配以开始位置这里我们可以看到在数组c 3,4,5元素中有第二个键分别是89,089,9089和匹配9089

请帮帮我 提前谢谢。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码块。

$a=array("pattern"=>"91919089");
$b=array("1"=>array("42","VIETNAM","84"),"2"=>array("46","3253","235"),"3"=>array("48","india","91"),"4"=>array("49","india","919"),"5"=>array("50","india","9191"));
$c=array("1"=>array("42","VIETNAM","84"),"2"=>array("46","3253","235"),"3"=>array("48","india","89"),"4"=>array("49","india","089"),"5"=>array("50","india","9089"));
        $count=0;
        $pos=0;
        $val=0;
        $arr=array();
        foreach ($b as $b1)
        {
            $count++;
            foreach ($b1 as $b2)
            {
                $len=strlen($b2);
                $string=$a['pattern'];
                $parts = array();
                for($i=0; $i<4; $i++)
                {
                    $parts[] = substr($string, 0, $len);
                    $string = substr($string, $len);
                }
                if($b2 == $parts[array_search($b2, $parts)])
                    $arr[]=array("pos"=>$count,"val"=>$b2);
            }
        }
        if(sizeof($arr)!=0)
        {
            foreach ($arr as $ele)
            {
                if($val < $ele['val'])
                {
                    $pos=$ele['pos'];
                    $val=$ele['val'];
                }
            }
            echo "Matching element is: " . $val . " at position " . $pos ."th in b.";
        }
        else
        {
            $count=0;
            foreach ($c as $c1)
            {
                $count++;
                foreach ($c1 as $c2)
                {
                    $len=strlen($c2);
                    $string=$a['pattern'];
                    $parts = array();
                    for($i=0; $i<4; $i++)
                    {
                        $parts[] = substr($string, 0, $len);
                        $string = substr($string, $len);
                    }
                    if($c2 == $parts[array_search($c2, $parts)])
                        $arr[]=array("pos"=>$count,"val"=>$c2);
                }
            }
            if(sizeof($arr)!=0)
            {
                foreach ($arr as $ele)
                {
                    if($val < $ele['val'])
                    {
                        $pos=$ele['pos'];
                        $val=$ele['val'];
                    }
                }
                echo "Matching element is: " . $val . " at position " . $pos ."th in c.";
            }
            else 
            {
                echo "Matching element not found in any of the array.";
            }
        }