2012-07-21 117 views
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]提到...

+0

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

+0

讀什麼[數組函數]的文檔(http://php.net/manual/en/ref.array.php)? – Jocelyn 2012-07-21 16:34:53

回答

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

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