Skip to content

Core\Ldap\Entry::toArray

$entry->toArray(): array<array<tainted bytes>>

Returns every attribute of an entry as an array. Each key is an attribute name, and each value is a list of the attribute's values as tainted bytes.

Use toArray when you do not know the attribute names in advance, for example to show every field of an entry or to save the entry as it is. When you know the name, string, strings and bytes are easier.

A key is written the way the server wrote the name, which can differ from the way you wrote it in select. Active Directory writes mail for MAIL, for example. The attributes are in the order the server sent them, and that order is not fixed.

Good to know: the DN is not in the array, so use dn for it. The array is a copy. A change to it does not change the entry.

Return value

Returns array<array<tainted bytes>>. The attributes, in the order the server sent them. The DN is not in the array, so use dn for it.

Examples

The examples below run with this configuration file.

nvs.toml
# These examples use the LDAP directory server on this computer at port 16636.
# `tls_ca_file` is the certificate that the server's certificate is checked against.
[ldap.corp]
url = "ldaps://127.0.0.1:16636"
base = "DC=example,DC=test"
user = "CN=Reader,OU=Company,DC=example,DC=test"
password = "Novis-reader-1"
tls_ca_file = "../../../../../tests/db/ca.crt"
timeout = "5s"
# The program may open the `corp` directory.
[capabilities.ldap]
connect = ["corp"]

Print every attribute

<?nvs
// Shows how to print every attribute of an entry with its number of values.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $person = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['mail', 'department', 'title', 'memberOf'] });
if ($person != null) {
// The server sends the attributes in no fixed order, so the lines are sorted.
array<tainted string> $lines = [];
foreach ($person->toArray() as tainted string $name => array<tainted bytes> $values) {
$lines[] = $name . ': ' . (Core\Arr::count($values) as string);
}
// This prints four lines. `memberOf` has 2 values, and the others have 1.
foreach (Core\Arr::sort($lines) as tainted string $line) {
echo $line, "\n";
}
}
Output
department: 1
mail: 1
memberOf: 2
title: 1

The server writes the names

<?nvs
// Shows that the keys of `toArray` are written the way the server writes them.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $person = $corp->read('CN=Ben Kohl,OU=Company,DC=example,DC=test', { select: ['MAIL', 'Department'] });
if ($person != null) {
// The program asked for `MAIL` and `Department`.
// This prints `department, mail`.
array<tainted string> $names = Core\Arr::sort(Core\Arr::keys($person->toArray()));
echo Core\Str::join($names, ', '), "\n";
// `string` finds a name in any case, so this prints `Warehouse`.
echo $person->string('DEPARTMENT') ?? 'no department', "\n";
}
Output
department, mail
Warehouse

The array is a copy

<?nvs
// Shows that a change to the array from `toArray` does not change the entry.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $person = $corp->read('CN=Dora Lind,OU=Company,DC=example,DC=test', { select: ['mail'] });
if ($person != null) {
// This removes every value of `mail` from the copy. Then it prints `0`.
array<array<tainted bytes>> $copy = $person->toArray();
$copy['mail'] = [];
echo Core\Arr::count($copy['mail']), "\n";
// The entry still has its value. This prints `dora.lind@example.test`.
echo $person->string('mail') ?? 'no mail address', "\n";
}
Output
0
dora.lind@example.test