PHP - 如何创建递归函数?

时间:2016-04-06 10:45:41

标签: php arrays

我的阵列:

array(1) {
  ["farm"] => array(1) {
    ["animals"] => array(1) {
      [horses] => array(4) {
        ["fred"] => string(4) "fred"
        ["sam"] => string(4) "sam"
        ["alan"] => string(4) "alan"
        ["john"] => string(4) "john"
      }
    }
  }
}

我的功能(由@FrayneKonok创建)

$id = "2";
$search = "horses";
get_values($arr);
function get_values($arr, $id) {
  global $search;
  foreach($arr as $key => $value) {
    if($key == $search) {
      if(is_array($value)) {
          echo(join("<a href='mypage.php?id=".$id."&dir=".$value."'><li>",array_keys($value)));
          echo("</li></a>");    
      } else {
        echo($value);
      }
    } else {
      get_values($value);
    }
  }
}
get_values($array,$id);

结果是:

"fred"
<a href="mypage.php?id=2&dir=Array"><li>sam</li></a>
<a href="mypage.php?id=2&dir=Array"><li>alan</li></a>
<a href="mypage.php?id=2&dir=Array"><li>john</li></a>

我正在寻找的结果:

<a href="mypage.php?id=2&dir=fred"><li>fred</li></a>
<a href="mypage.php?id=2&dir=sam"><li>sam</li></a>
<a href="mypage.php?id=2&dir=alan"><li>alan</li></a>
<a href="mypage.php?id=2&dir=john"><li>john</li></a>

另一个例子是,当我使用if($search = "farm")时,我的结果变为:

"animals"

当我要找的结果是:

<a href="mypage.php?id=2&dir=animals"><li>animals</li></a>

2 个答案:

答案 0 :(得分:1)

Online link

数组和输入,函数调用

$arr = array("farm" => 
             array("animals"=>
                   array("horses" => 
                         array("fred" => "fred",
                               "sam" => "sam",
                               "alan" => "alan",
                               "john" => "john")
                        )
                  )
            );


$search = 'farm';
get_values($arr);

<强>功能:

function get_values($arr){  
    global $search;
    foreach($arr as $key => $value){
        if($key == $search){
            if(is_array($value)){
                $keys = array_keys($value);
                if(count($keys) > 1){
                    for($i = 0; $i < count($keys); $i++){
                        echo '<a href="mypage.php?id=2&dir='.$keys[$i].'"><li>'.$keys[$i].'</li></a>';
                    }
                }else{
                    echo '<a href="mypage.php?id=2&dir='.$keys[0].'"><li>'.$keys[0].'</li></a>';
                }
            }           
            else{
                echo $value;
            }
        }else{
            get_values($value);
        }       
    }   
}

<强>输出

<a href="mypage.php?id=2&dir=animals"><li>animals</li></a>

还测试了horses

答案 1 :(得分:1)

我建议使用其他foreach

而不是

if(is_array($value)){
    echo join("<a href='mypage.php?id=".$id."&dir=".$value."'><li>",array_keys($value));
    echo "</li></a>";    
}       

使用

if(is_array($value)){
    foreach( $value as $k => $v ) {
        echo "<a href='mypage.php?id=".$id."&dir=".$k."'><li>".$k."</li></a>\n";
    }   
}           

另外,不要忘记每次都通过$id

整个事情变成了:

$arr = array( "farm"=> array(  "animals"=> array( "horses"=> array( "fred" => "fred", "sam" => "sam", "alan" => "alan", "john" => "john" ) ) ) );

$id = "2";
 $search = "horses";
    get_values($arr, $id); // <-- pass $id here

    function get_values($arr, $id){  
        global $search;
        foreach($arr as $key => $value){
            if($key == $search){
                if(is_array($value)){
                    foreach( $value as $k => $v ) {
                        echo "<a href='mypage.php?id=".$id."&dir=".$k."'><li>".$k."</li></a>\n";
                    }   
                }           
                else{
                    echo $value;  
                }
            }else{
                get_values($value, $id ); // <-- pass $id here to 
            }      
        }      
    }