Create an LDAP Address Book with PHP - Format Output (Page 3 of 6 )
When an LDAP search is performed, the data is returned in whatever sequence it is found. In other words, there is not an easy way to sort the data like with a common SQL ORDER BY statement. As well, many public LDAP directories do not have standard capitalization.
Since the sort is based on the ASCII value of the strings, we must format the strings with all lowercase letters to appropriately alphabetize our output.
It is important to note, that an LDAP result set is returned as a multi-dimensional array. Thus, at this point in our script $result_array contains something like this:
In our example, $format_array is our new array which contains the query results in a format optimized for output.
First, we loop through every element of the $result_array and assign it to a two-dimensional array for sorting purposes. At the same time we are using the strtolower() function to make all values lower-case.
Second, we sort the array using a handy little search algorithm provided by PHP called sort(). The first argument is the array. The second is what type of sorting to perform, as defined by the PHP documentation. Since we are sorting by string, we use "SORT_STRING".
Third, we loop through the newly formatted array and assign it to an output string named $result_list that contains the HTML representation of the data. It is important to note that I have used the ldap URL format for the hyper-links. An example of this looks something like this: