【发布时间】:2020-09-04 19:36:23
【问题描述】:
我有这段代码,请随意跳到代码底部,我正在尝试遍历数组,我留下了整个代码,以便将其用于测试目的。
<?php
//Saves all the attributes of all entries into an array.
function cleanUpEntry( $entry ) {
$retEntry = array();
for ( $i = 0; $i < $entry["count"]; $i++ ) {
if (is_array($entry[$i])) {
$subtree = $entry[$i];
//This condition should be superfluous so just take the recursive call
//adapted to your situation in order to increase perf.
if ( ! empty($subtree['dn']) and ! isset($retEntry[$subtree['dn']])) {
$retEntry[$subtree['dn']] = cleanUpEntry($subtree);
}
else {
$retEntry[] = cleanUpEntry($subtree);
}
}
else {
$attribute = $entry[$i];
if ( $entry[$attribute]['count'] == 1 ) {
$retEntry[$attribute] = $entry[$attribute][0];
} else {
for ( $j = 0; $j < $entry[$attribute]['count']; $j++ ) {
$retEntry[$attribute][] = $entry[$attribute][$j];
}
}
}
}
return $retEntry;
}
$ldaprdn = "cn=read-only-admin,dc=example,dc=com";
$ldappass ="password";
$ldapuri = "ldap.forumsys.com";
// Connecting to LDAP
$ldapconn = ldap_connect($ldapuri)
or die("That LDAP-URI was not parseable");
//We need to set the LDAP Protocol Version or else it isn't able to bind properly to the LDAP server.
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
//We bind to the LDAP server using the previous credentials and location.
$ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
// Verify if bind was successful or not
if ($ldapbind) {
echo "LDAP bind successful...\n";
} else {
echo "LDAP bind failed...\n";
}
//Gives where to search & what to search for
$dn = "dc=example,dc=com";
$filter = "(objectclass=*)";
//Saves the result into result variable
$result = ldap_search($ldapconn, $dn, $filter);
$info = ldap_get_entries($ldapconn, $result);
$header = array_fill_keys($headers, null);
$output = [];
//Gets the search results and saves into details variable.
$details = cleanUpEntry($info);
print_r($details);
//HERE'S MY ISSUE: The array isn't being iterated through properly.
foreach ( $details as $name => $entry ){
echo "\nName: " . $name . "\n";
if(is_array($entry)){
foreach (array_keys($entry) as $array_key => $value)
//echo "Entry: " . $array_key . ":" . $value . "\n";
echo $value . "\n";
//This foreach doesn't work.
/*foreach ($value as $key1 => $value2){
echo $key1.":".$value;
}*/
}else{
foreach($entry as $key => $value){
echo $key . ":" . $value . "\n";
}
//echo "Entry: " . $entry . "\n";
}
}
?>
这是数组的样子,我把大部分元素都去掉了,但是如果你复制我上面的原始代码,你可以看到整个数组:
Array
(
[dc=example,dc=com] => Array
(
[objectclass] => Array
(
[0] => top
[1] => dcObject
[2] => organization
)
[o] => example.com
[dc] => example
)
[cn=admin,dc=example,dc=com] => Array
(
[objectclass] => Array
(
[0] => simpleSecurityObject
[1] => organizationalRole
)
[cn] => admin
[description] => LDAP administrator
)
[uid=newton,dc=example,dc=com] => Array
(
[sn] => Newton
[objectclass] => Array
(
[0] => inetOrgPerson
[1] => organizationalPerson
[2] => person
[3] => top
)
[uid] => newton
[mail] => newton@ldap.forumsys.com
[cn] => Isaac Newton
)
基本上,我目前的输出是:
Name: dc=example,dc=com
objectclass
o
dc
Name: cn=admin,dc=example,dc=com
objectclass
cn
description
Name: uid=newton,dc=example,dc=com
sn
objectclass
uid
mail
cn
应该是这样的:
Name: dc=example,dc=com
objectclass:top
objectclass: dcObject
objectclass: organization
o: example.com
dc: example
Name: cn=admin,dc=example,dc=com
objectclass: simpleSecurityObject
objectclass: organizationalRole
cn: admin
description: LDAP administrator
Name: uid=newton,dc=example,dc=com
sn: Newton
objectclass: inetOrgPerson
objectclass: organizationalPerson
objectclass: person
objectclass: top
uid: newton
mail: newton@ldap.forumsys.com
cn: Isaac Newton
如果您也可以将其转换为 JSON 和 BSON,则可以加分,因为这将用于添加到 MongoDB
【问题讨论】:
-
你看过
json_encode()吗? -
这不是有效的 JSON 格式,因为您有重复的键(对象类)。
-
这个问题的主要原因是遍历数组,因为这是我目前主要关心的问题。至于 JSON 格式,它看起来像这样:“objectclass”:“{top, dcObject, person}”。
标签: php arrays multidimensional-array active-directory ldap