我想從數組中檢索一個鍵「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"));
?>