如何打印通过URL参数检测到的特定阵列的所有子项?

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

And this is my URL

mypage.php?id=2&dir=animals

I would like to print the children of my URL parameter dir(In this case:animals)

This is the way I try to do it:

 foreach($array as $sub) {
        if ($_GET['dir'] == $sub){
        $result = array_merge($result, $sub);
        echo $result;
        }
}

My result: An empty page.

The result I wish: horses

Your array:

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

Here we go, i make a recursive function for searching the value.

This function not work if you search for fred and their siblings.

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

function get_values($arr){  
    global $search;
    foreach($arr as $key => $value){
        if($key == $search){
            if(is_array($value)){
                echo join(", ", array_keys($value));
            }           
            else{
                echo $value;
            }
        }else{
            get_values($value);
        }       
    }   
}

Output:

fred, sam, alan, john

Your $array has a farm key and that farm only contains your dir animals.

If everything is in farm you can do like this:

if(!empty($_GET['dir'])) {
  $result = array_merge($result, $array['form'][$_GET['dir']]
}
print_r($result);

I don't know what $result contains initially, but you can adapt if this is not the case or just echo $array['form'][$_GET['dir']] if you don't have multiple items in $result