dt3999 2014-04-10 08:20
浏览 63

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

I have multi dimensional array and i have another array

CODE :

SINGLE ARRAY calles as A

Array
(
    [pattern] => 91919089
)

MULTIDIMENSIONAL ARRAY called as 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
    )

Multi dimensional array called as 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
    )

Here we can see that pattern 91919089 is matched with 3rd 4th and 5th element of array b and key of 2 of each is 91,919 and 9191 respectively and 5[2]i want result that 9191. = 9191 which is maximum matching string of pattern 91919089 so result is 5

suppose if i will remove 5th element from multi dimensional array then must give me result that 4th is matching with 919. and if no match found then need to match from another array c from end to start postion here we can see in array c 3,4,5 element have 2nd keys are 89,089,9089 respectively and match 9089

Please help me out Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongsun5330 2014-04-10 10:21
    关注

    Please try following code block.

    $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.";
                }
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向