I have a multidimensional array where I would like to remove a specific layer of the data. Basically, I would like to remove all the labels that are numeric aka the [0] => Array, [1] => Array, [2] => Array, and [3] => Array.
Here’s the array I have currently:
Array ( [0] => Array ( [CN=Abraham Lincoln,CN=Users,DC=test,DC=io] => Array ( [objectclass] => Array ( [0] => top [1] => person [2] => organizationalPerson [3] => user ) [cn] => Abraham Lincoln ) ) [1] => Array ( [CN=Administrator,CN=Users,DC=test,DC=io] => Array ( [objectclass] => Array ( [0] => top [1] => person [2] => organizationalPerson [3] => user ) [distinguishedname] => CN=Administrator,CN=Users,DC=test,DC=io ) ) [2] => Array ( [CN=CloudNCUsers,CN=Users,DC=test,DC=io] => Array ( [objectclass] => Array ( [0] => top [1] => group ) [distinguishedname] => CN=CloudNCUsers,CN=Users,DC=test,DC=io ) ) [3] => Array ( [CN=Jill Dope,CN=Users,DC=test,DC=io] => Array ( [objectclass] => Array ( [0] => top [1] => person [2] => organizationalPerson [3] => user ) [distinguishedname] => CN=Jill Dope,CN=Users,DC=test,DC=io ) ) )
Here’s the array I need:
Array ( [CN=Abraham Lincoln,CN=Users,DC=test,DC=io] => Array ( [objectclass] => Array ( [0] => top [1] => person [2] => organizationalPerson [3] => user ) [cn] => Abraham Lincoln ) ) [CN=Administrator,CN=Users,DC=test,DC=io] => Array ( [objectclass] => Array ( [0] => top [1] => person [2] => organizationalPerson [3] => user ) [distinguishedname] => CN=Administrator,CN=Users,DC=test,DC=io ) [CN=CloudNCUsers,CN=Users,DC=test,DC=io] => Array ( [objectclass] => Array ( [0] => top [1] => group ) ) [CN=Jill Dope,CN=Users,DC=test,DC=io] => Array ( [objectclass] => Array ( [0] => top [1] => person [2] => organizationalPerson [3] => user ) [distinguishedname] => CN=Jill Dope,CN=Users,DC=test,DC=io ) )
Any help would be appreciated, thanks!
Advertisement
Answer
You can remove a level from the array using key
and reset
and saving to a new array:
$result = array(); foreach ($entries as $entry) { $result[key($entry)] = reset($entry); }
You should be able to do the same thing when you initially load the array to save this extra step:
$entry = ldap_get_entries($ldapconnection, $result); $array[key($entry)] = reset($entry);
Note that this assumes that all of the key values (CN=Abraham Lincoln,CN=Users,DC=test,DC=io
etc.) are unique. If they are not, later values in the array will overwrite earlier ones in the output. Note also that if ldap_get_entries
returns more than one value, this will remove all but the first. However based on your existing code and the sample data you have provided, it would appear that this is not the case.