2

I am trying to retrieve value of a key (dn) from an array.

Here is my code: https://ideone.com/3gUfWs

Output getting as:

array (
  'cn' => 
  array (
    'count' => 1,
    0 => 'abcd',
  ),
  0 => 'cn',
  'count' => 1,
  'dn' => 'cn=abcd,ou=test,dc=myproj,dc=com',
)

But What I need output as:

cn=abcd,ou=test,dc=myproj,dc=com

BTW, here is my same code provided in above link:

$cat = array(
  "Name"    => "Percy",
  "Colour"  => "Black",
  "Hobbies" => array(
    1 => "Chasing mice",
    2 => "Sleeping",
    3 => "Prowling"
  ),
   "Name"    => "Jackson",
);
 
 
$cat2 = array(
    'count' => 1,
    0 => array(
        'cn' => array(
            'count' => 1,
            0 => 'abcd',
        ) ,
        0 => 'cn',
        'count' => 1,
        'dn' => 'cn=abcd,ou=test,dc=myproj,dc=com',
    ) ,
);
 
$output = "";
// Find the value of a Key
function seekKey($haystack, $needle){
    global $output;
  foreach($haystack as $key => $value){
    if($key == $needle){
      $output = $value;
 
    }elseif(is_array($value)){
      $output = seekKey($value, $needle);
 
    }
  }
  return $output;
}
 
var_export(seekKey($cat2,"dn"));
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
learner
  • 6,062
  • 14
  • 79
  • 139

4 Answers4

3

Just need to change if($key == $needle){ to if($key === $needle){

Answer to why? is here.

Community
  • 1
  • 1
viral
  • 3,724
  • 1
  • 18
  • 32
0

Here is my implementation. This will return the first result of $needle key in the array recursively.

function seekKey($haystack, $needle){
  foreach( $haystack as $key => $value ) {
    if($key === $needle){
      return $value;
    } elseif ( is_array($value) ) {
      $val = seekKey($value, $needle);

      if ( $val !== false )
        return $val;
    }
  }
  return false;
}

You can also see it on the playground

There's also a good post here on another way to search for a $needle.

function seekKey(array $array, $needle)
{
    $iterator  = new RecursiveArrayIterator($array);
    $recursive = new RecursiveIteratorIterator($iterator,
                         RecursiveIteratorIterator::SELF_FIRST);
    foreach ($recursive as $key => $value) {
        if ($key === $needle) {
            return $value;
        }
    }
}
Community
  • 1
  • 1
Blue
  • 22,608
  • 7
  • 62
  • 92
0

You may try this

 [akshay@localhost tmp]$ cat test.php
 <?php

 $cat2 = array(
     'count' => 1,
     0 => array(
         'cn' => array(
             'count' => 1,
             0 => 'abcd',
         ) ,
         0 => 'cn',
         'count' => 1,
         'dn' => 'cn=abcd,ou=test,dc=myproj,dc=com',
         'test'=>array('another'=>array('so'=>'stack overflow'))
     ) 
 );


 function seekKey($arr, $to_find)
 {
     foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($arr), RecursiveIteratorIterator::SELF_FIRST) as $key => $value)
     {
         if ($key === $to_find)
             return $value;
     }
 }


 // Input
 print_r($cat2);

 // Output
 print  seekKey($cat2,"dn")."\n";

 print  seekKey($cat2,"so")."\n";

 ?>

Output

 [akshay@localhost tmp]$ php test.php
 Array
 (
     [count] => 1
     [0] => Array
         (
             [cn] => Array
                 (
                     [count] => 1
                     [0] => abcd
                 )

             [0] => cn
             [count] => 1
             [dn] => cn=abcd,ou=test,dc=myproj,dc=com
             [test] => Array
                 (
                     [another] => Array
                         (
                             [so] => stack overflow
                         )

                 )

         )

 )

 cn=abcd,ou=test,dc=myproj,dc=com
 stack overflow
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
-1

You can get value like this $dn = $cat2[0]['dn'];

Mohamed Belal
  • 610
  • 7
  • 11