0

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

7
  • I found out that I need to write foreach($array as $sub => $key) { but then my result is farm Commented Apr 5, 2016 at 15:14
  • If you want to use key it's the other way. foreach($array as $key => $sub) {...} Commented Apr 5, 2016 at 15:15
  • @Jarla, What will be the result if the dir is animals?? Commented Apr 5, 2016 at 15:17
  • @FrayneKonok If dir is animals, the result should be horses Commented Apr 5, 2016 at 15:18
  • 1
    okey, wait for result Commented Apr 5, 2016 at 15:22

2 Answers 2

1

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

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

7 Comments

One question, if I want to write instead of fred, sam, alan, john <li>fred</li><li>sam</li><li>alan<l/i><li>john</li>. How do I do that?
I do not know if this is the best way: if(is_array($value)){ echo "<li>"; echo join("</li><li>",array_keys($value)); echo "</li>"; }
I get some problems when I want to make a link: if(is_array($value)){ echo "<a href='mypage.php?id=".$id."&dir=".value."'><li>"; echo join("</li></a><a href='mypage.php?id=".$id."&dir=".value."'><li>",array_keys($value)); echo "</li></a>"; } the value in the link is always printing the word Array instead of the for example fred. And do I have to write the links twice?
Let's see tomorrow. Now I am sleeping.
Thank you very much, my problem is that I need as a result the same value inside the link of each like <li><a href="mypage.php?dir=fred">fred</a></li><li><a href="mypage.php?dir=sam">sam</a></li> I cannot get the second fred between it. But you helped me already enough, so I will make a new question for it :)
|
0

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

5 Comments

what to do if the dir is horses.
@Frayne Konok yes it is possible, that dir is horses, also that dir is farm
From question is not clear if dir can be anything or just keys from farm. The solution from above is just for farm items. It needs to be adapted to check everywhere.
@DanielDudas dir can be anything
I tried to use your code, but I get an empty white page

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.