php需要从数组中检索键:'dn'值

php需要从数组中检索键:'dn'值

问题描述:

I am trying to retreive 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:

<?php

$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"));

?>

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

Answer to why? is here.

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

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

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")."
";

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

 ?>

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

(c) not mine

$sNeededKey = 'dn';
preg_match('/s\:[\d]+\:\"'.preg_quote($sNeededKey).'\";s\:[\d]+\:\"(.*?)\"/', serialize($cat2), $rgMatches);
echo $sResult    = $rgMatches[1];

result

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