在数组中搜索文本

时间:2012-07-21 16:14:35

标签: php arrays

<<
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => soccer player
                    [1] => soccer player athlete who play soccer
                    [2] => sport
                    [3] => soccer
                    [4] => soccer football game in which two team of 11 player try to kick or head ball into opponent goal
                )

            [1] => Array
                (
                    [0] => soccer player
                    [1] => soccer player athlete who play soccer
                    [2] => play
                    [3] => activity
                    [4] => actor
                )

            [2] => Array
                (
                    [0] => soccer player
                    [1] => soccer player athlete who play soccer
                    [2] => player
                    [3] => commercial enterprise activity of provide good and service involve financial and commercial and industrial aspect
                    [4] => player person who participate in or be skilled at some game
                )

            [3] => Array
                (
                    [0] => soccer player
                    [1] => soccer player athlete who play soccer
                    [2] => athlete person train to compete in sport
                    [3] => athlete person train to compete in sport
                    [4] => athlete person train to compete in sport
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => athlete
                    [1] => athlete person train to compete in sport
                    [2] => sport
                    [3] => sport
                    [4] => sport
                )

            [1] => Array
                (
                    [0] => athlete
                    [1] => athlete person train to compete in sport
                    [2] => compete compete for something
                    [3] => compete compete for something
                    [4] => compete compete for something
                )

            [2] => Array
                (
                    [0] => athlete
                    [1] => athlete person train to compete in sport
                    [2] => train
                    [3] => train create by train and teach
                    [4] => train public transport provide by line of railway car couple together and draw by locomotive
                )

            [3] => Array
                (
                    [0] => athlete
                    [1] => athlete person train to compete in sport
                    [2] => person
                    [3] => person
                    [4] => person
                )

            [4] => Array
                (
                    [0] => athlete
                    [1] => athlete person train to compete in sport
                    [2] => athlete person train to compete in sport
                    [3] => athlete person train to compete in sport
                    [4] => athlete person train to compete in sport
                )

        )

>>

你好

我需要上面显示的数组的php搜索代码。但搜索不应该是一个完全匹配的字符串,例如我正在搜索"11 player"它应该给我Array[0]中提到的结果Array[0][0][4] ...

1 个答案:

答案 0 :(得分:0)

你需要一个简单的递归搜索功能。

<?php
    function recursive_search($array,$text,$path = "")
    {
        $result = array();
        foreach($array as $index => $value)
        {
            if(is_array($value))
                $result = array_merge($result,recursive_search($value,$text,$path . '/' . $index));
            else if(strpos($value,$text) !== false)
                $result[] = array('path' => $path . '/' . $index,'value' => $value); 
                //$result[] = $value; // use this line and delete above if you dont want path.


        }

        return $result;

    }

用法

 $my_array =
    array(
        array(
            array(
                "soccer player",
                "soccer player athlete who play soccer",
                "player",
                "commercial enterprise activity of provide good and service involve financial and commercial and industrial aspect",
                "player person who participate in or be skilled at some game",
                "soccer football game in which two team of 11 player try to kick or head ball into opponent goal"
            )
        ) ,
        array(
            "soccer player",
            "soccer player athlete who play soccer",
            "player",
            "commercial enterprise activity of provide good and service involve financial and commercial and industrial aspect",
            "player person who participate in or be skilled at some game",
            "soccer football game in which two team of 11 player try to kick or head ball into opponent goal"
        )
    );



    $elements = recursive_search($my_array,"11 player");

    var_dump($elements);

结果:

Array
(
    [0] => Array
        (
            [path] => /0/0/5
            [value] => soccer football game in which two team of 11 player try to kick or head ball into opponent goal
        )

    [1] => Array
        (
            [path] => /1/5
            [value] => soccer football game in which two team of 11 player try to kick or head ball into opponent goal
        )

)

如果你想要没有路径输出的元素将是那样的

Array
(
    [0] => soccer football game in which two team of 11 player try to kick or head ball into opponent goal
    [1] => soccer football game in which two team of 11 player try to kick or head ball into opponent goal
)