0
<<
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]

4

1 に答える 1

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
)
于 2012-07-21T16:44:27.543 に答える