2015-05-26 40 views
2

我想從數組中檢索一個鍵「dn」的值。PHP需要檢索密鑰:從陣列的'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")); 

?> 

回答

3

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

答到爲什麼呢?here

-1

你可以得到這樣$dn = $cat2[0]['dn'];

0

值這裏是我的實現。這將遞歸地返回數組中$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; 
     } 
    } 
} 
0

你可以試試這個

[[email protected] 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"; 

?> 

輸出

[[email protected] 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 
0

(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