fork download
  1. <?php
  2.  
  3. $cat = array(
  4. "Name" => "Percy",
  5. "Colour" => "Black",
  6. "Hobbies" => array(
  7. 1 => "Chasing mice",
  8. 2 => "Sleeping",
  9. 3 => "Prowling"
  10. ),
  11. "Name" => "Jackson",
  12. );
  13.  
  14.  
  15. $cat2 = array(
  16. 'count' => 1,
  17. 0 => array(
  18. 'cn' => array(
  19. 'count' => 1,
  20. 0 => 'abcd',
  21. ) ,
  22. 0 => 'cn',
  23. 'count' => 1,
  24. 'dn' => 'cn=abcd,ou=test,dc=myproj,dc=com',
  25. ) ,
  26. );
  27.  
  28. $output = "";
  29. // Find the value of a Key
  30. function seekKey($haystack, $needle){
  31. global $output;
  32. foreach($haystack as $key => $value){
  33. if($key == $needle){
  34. $output = $value;
  35.  
  36. }elseif(is_array($value)){
  37. $output = seekKey($value, $needle);
  38.  
  39. }
  40. }
  41. return $output;
  42. }
  43.  
  44. var_export(seekKey($cat2,"dn"));
  45.  
  46. ?>
Success #stdin #stdout 0.02s 24448KB
stdin
Standard input is empty
stdout
array (
  'cn' => 
  array (
    'count' => 1,
    0 => 'abcd',
  ),
  0 => 'cn',
  'count' => 1,
  'dn' => 'cn=abcd,ou=test,dc=myproj,dc=com',
)