文本在阵列搜索

问题描述:

<< 
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]提到...

+0

我没有得到一个想法如何做它 – 2012-07-21 16:23:43

+0

读什么[数组函数]的文档(http://php.net/manual/en/ref.array.php)? – Jocelyn 2012-07-21 16:34:53

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

<?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 
) 
+0

thanx很多布拉沃! – 2012-07-22 12:41:25