简体   繁体   English

PHP - 如何创建递归函数?

[英]PHP - How can I create a recursive function?

My array:我的阵列:

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"
      }
    }
  }
}

My function (created by @FrayneKonok)我的函数(由@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);

The result is:结果是:

"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>

The result I'm looking for:我正在寻找的结果:

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

Another example is when i use if($search = "farm") my result becomes:另一个例子是当我使用if($search = "farm")我的结果变成:

"animals"

When the result I'm looking for is:当我正在寻找的结果是:

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

Online link在线链接

Array and input, function call数组和输入,函数调用

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


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

Function:功能:

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);
        }       
    }   
}

Output输出

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

Also tested for the horses .还测试了horses

I suggest using another foreach我建议使用另一个foreach

Instead of代替

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

Use

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

Also don't forget to pass $id each time也不要忘记每次传递$id

The whole thing becomes:整个事情变成了:

$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 
            }      
        }      
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM