【问题标题】:php need to retrieve key: 'dn' value from an arrayphp需要从数组中检索键:'dn'值
【发布时间】:2015-08-07 20:54:13
【问题描述】:

我正在尝试从数组中检索键“dn”的值。

这是我的代码:https://ideone.com/3gUfWs

输出如下:

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

但我需要输出为: cn=abcd,ou=test,dc=myproj,dc=com

顺便说一句,这是我在上面链接中提供的相同代码:

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

?>

【问题讨论】:

    标签: php


    【解决方案1】:

    (c) 不是我的

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

    结果

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

    【讨论】:

      【解决方案2】:

      你可以试试这个

       [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";
      
       ?>
      

      输出

       [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
      

      【讨论】:

        【解决方案3】:

        只需要把if($key == $needle){改成if($key === $needle){

        为什么?的答案是here

        【讨论】:

          【解决方案4】:

          这是我的实现。这将递归返回数组中$needle 键的第一个结果。

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

          你也可以在playground看到它

          还有一个很好的帖子here 以另一种方式搜索$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;
                  }
              }
          }
          

          【讨论】:

            【解决方案5】:

            你可以获得这样的价值$dn = $cat2[0]['dn'];

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2020-02-10
              • 1970-01-01
              • 1970-01-01
              • 2012-01-16
              • 2019-08-25
              • 2019-03-13
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多