1 回答
TA贡献1869条经验 获得超4个赞
假设只有两个级别,您可以轻松执行 2 次foreach 循环:
<?php
$arr = [
'dc=example,dc=com' => [
'objectclass' => [
'top',
'dcObject',
'organization'
],
'o' => 'example.com',
'dc' => 'example'
],
'cn=admin,dc=example,dc=com' => [
'objectclass' => [
'simpleSecurityObject',
'organizationalRole'
],
'cn' => 'admin',
'description' => 'LDAP administrator'
],
'uid=newton,dc=example,dc=com' => [
'sn' => 'Newton',
'objectclass' => [
'inetOrgPerson',
'organizationalPerson',
'person',
'top'
],
'uid' => 'newton',
'mail' => 'newton@ldap.forumsys.com',
'cn' => 'Isaac Newton'
]
];
foreach($arr as $name => $settings) {
$new[] = PHP_EOL."Name: {$name}";
foreach($settings as $k => $v) {
$new[] = "{$k}: ".((is_array($v))? implode(PHP_EOL."{$k}: ", $v) : $v);
}
}
print_r(trim(implode(PHP_EOL, $new)));
会给你:
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,您可能只需使用json_encode($arr)自动将其设为json(如评论中所述)。
- 1 回答
- 0 关注
- 92 浏览
添加回答
举报