2

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)

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

"animals"

When the result I'm looking for is:

<a href="mypage.php?id=2&dir=animals"><li>animals</li></a>
4
  • let me double check again. Commented Apr 6, 2016 at 10:47
  • is your id is a fixed thing??? Commented Apr 6, 2016 at 10:48
  • 1
    @Jarla some of your lines had quite a bit of unnecessary white-space. With large files excessive white-space this has the possibility to stump performance. Also In the line ["sam"] => string(4) "sam" the length of "sam" is not 3 its 4 be careful, this could lead to errors. Commented Apr 6, 2016 at 11:01
  • i did it, You can test it now. Commented Apr 6, 2016 at 11:07

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

I suggest using another 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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.