Bluehost.com Web Hosting $6.95

Sorting Multidimensional Array?

This is a discussion on Sorting Multidimensional Array? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi I've performed a query on Active Directory to pull usernames & mobile numbers, using: <snip> $read = ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-29-2007
benb
 
Posts: n/a
Default Sorting Multidimensional Array?

Hi
I've performed a query on Active Directory to pull usernames & mobile
numbers, using:

<snip>
$read = ldap_search($connect, $base_dn, $filter, $inforequired)
$info = ldap_get_entries($connect, $read);

This has returned an array (not sure of the type, is it multidimensional, or
an array of arrays?) with 2 values:

$info[$i]["displayname"][0]
and
$info[$i]["mobile"][0]

When I print this information in a table I get the following

John Doe 077654321
Joe Blogs 071234567
Alan Smith 077711223
....etc

How do I go about sorting this array, so that [displayname] is alphabetical?
I've tried using sort($info) but this seems to wipe the array, I've also
tried sort($info[0]["displayname"]) but that doesn't seem to sort anything!

Any help, or suggestions much appreciated!

Ben


Reply With Quote
  #2 (permalink)  
Old 01-29-2007
Colin McKinnon
 
Posts: n/a
Default Re: Sorting Multidimensional Array?

benb wrote:

> <snip>
> $read = ldap_search($connect, $base_dn, $filter, $inforequired)
> $info = ldap_get_entries($connect, $read);
>
> This has returned an array (not sure of the type, is it multidimensional,
> or an array of arrays?) with 2 values:
>
> $info[$i]["displayname"][0]
> and
> $info[$i]["mobile"][0]
>

<snip>
>
> How do I go about sorting this array, so that [displayname] is
> alphabetical? I've tried using sort($info) but this seems to wipe the
> array, I've also tried sort($info[0]["displayname"]) but that doesn't seem
> to sort anything!
>


You'll need to write your own comparison function (not tested - YMMV):


function my_sort(&$a, &$b)
{
global $sort_by;
if ($a[$sort_by][0]==$b[$sort_by][0]) return 0;
return ($a[$sort_by][0]<$b[$sort_by][0]) ? -1 : 1;
}

$sort_by = 'displayname';
usort($info, 'my_sort');
// or if you prefer...
$sort_by = 'mobile';
usort($info, 'my_sort');



Note that you don't have to worry about iterating through th array yourself,
or having to code your own quicksort when you discover how slow a bubble
sort really is...etc.

HTH

C.
Reply With Quote
  #3 (permalink)  
Old 01-29-2007
bob.chatman@gmail.com
 
Posts: n/a
Default Re: Sorting Multidimensional Array?

On Jan 29, 7:25 am, "benb" <b...@nospam.postalias> wrote:
> Hi
> I've performed a query on Active Directory to pull usernames & mobile
> numbers, using:
>
> <snip>
> $read = ldap_search($connect, $base_dn, $filter, $inforequired)
> $info = ldap_get_entries($connect, $read);
>
> This has returned an array (not sure of the type, is it multidimensional, or
> an array of arrays?) with 2 values:
>
> $info[$i]["displayname"][0]
> and
> $info[$i]["mobile"][0]
>
> When I print this information in a table I get the following
>
> John Doe 077654321
> Joe Blogs 071234567
> Alan Smith 077711223
> ...etc
>
> How do I go about sorting this array, so that [displayname] is alphabetical?
> I've tried using sort($info) but this seems to wipe the array, I've also
> tried sort($info[0]["displayname"]) but that doesn't seem to sort anything!
>
> Any help, or suggestions much appreciated!
>
> Ben


Sort treats the array as though it doesnt have keys. The keys and
values are mixed together and will become one giant array of =P

if you want to sort the keys alphabetically you should use ksort()
http://us2.php.net/manual/en/function.ksort.php

and if you would like to make your own sort algorthm you can use
usort()
http://us2.php.net/manual/en/function.usort.php

Reply With Quote
  #4 (permalink)  
Old 01-29-2007
Gleep
 
Posts: n/a
Default Re: Sorting Multidimensional Array?

On Mon, 29 Jan 2007 15:25:45 -0000, "benb" <benb@nospam.postalias> wrote:

>Hi
>I've performed a query on Active Directory to pull usernames & mobile
>numbers, using:
>
><snip>
>$read = ldap_search($connect, $base_dn, $filter, $inforequired)
>$info = ldap_get_entries($connect, $read);
>
>This has returned an array (not sure of the type, is it multidimensional, or
>an array of arrays?) with 2 values:
>
>$info[$i]["displayname"][0]
>and
>$info[$i]["mobile"][0]
>
>When I print this information in a table I get the following
>
>John Doe 077654321
>Joe Blogs 071234567
>Alan Smith 077711223
>...etc
>
>How do I go about sorting this array, so that [displayname] is alphabetical?
>I've tried using sort($info) but this seems to wipe the array, I've also
>tried sort($info[0]["displayname"]) but that doesn't seem to sort anything!
>
>Any help, or suggestions much appreciated!
>
>Ben
>





I wanted to copy a few pages from Sams Web development 3rd edition
but motherfuckin adobe reader won't let me copy it.

I've used this technique in some applications i have. You can sort a multidimentionsl array with
this function.

function compare($x, $y)
{
if ($x[1] == $y[1])
return 0;
else if ($x[1] < $y[1])
return -1;
else
return 1;
}

usort ($products, 'compare')

$products would be your multidem array
you can reverse the sort by changing the < to >


if you need more explaination look for book
PHP and MySQL Web Development
Luke Welling / Laura Thomson
This is the book I used to get started with PHP

one of the problems I see is that this will sort on the first name, not the last name
a work around would be to split out the names and recreate another name array
however another problem could surface - what if people use a 3rd name or a hyphen
you have to expect all kind of odd ball scenarios and account for it early in your application.






Reply With Quote
  #5 (permalink)  
Old 01-30-2007
benb
 
Posts: n/a
Default Re: Sorting Multidimensional Array?

"Colin McKinnon"
<colin.thisisnotmysurname@ntlworld.deletemeunlessU RaBot.com> wrote in
message news:Yotvh.85063$z01.32348@newsfe3-gui.ntli.net...
> benb wrote:
>
>> <snip>
>> $read = ldap_search($connect, $base_dn, $filter, $inforequired)
>> $info = ldap_get_entries($connect, $read);
>>
>> This has returned an array (not sure of the type, is it multidimensional,
>> or an array of arrays?) with 2 values:
>>
>> $info[$i]["displayname"][0]
>> and
>> $info[$i]["mobile"][0]
>>

> <snip>
>>
>> How do I go about sorting this array, so that [displayname] is
>> alphabetical? I've tried using sort($info) but this seems to wipe the
>> array, I've also tried sort($info[0]["displayname"]) but that doesn't
>> seem
>> to sort anything!
>>

>
> You'll need to write your own comparison function (not tested - YMMV):
>
>
> function my_sort(&$a, &$b)
> {
> global $sort_by;
> if ($a[$sort_by][0]==$b[$sort_by][0]) return 0;
> return ($a[$sort_by][0]<$b[$sort_by][0]) ? -1 : 1;
> }
>
> $sort_by = 'displayname';
> usort($info, 'my_sort');
> // or if you prefer...
> $sort_by = 'mobile';
> usort($info, 'my_sort');
>
>
>
> Note that you don't have to worry about iterating through th array
> yourself,
> or having to code your own quicksort when you discover how slow a bubble
> sort really is...etc.
>
> HTH
>
> C.


Hi Colin,

Thanks for the code, I shall give it a try and report back!

Ben


Reply With Quote
  #6 (permalink)  
Old 01-30-2007
benb
 
Posts: n/a
Default Re: Sorting Multidimensional Array?

"Colin McKinnon"
<colin.thisisnotmysurname@ntlworld.deletemeunlessU RaBot.com> wrote in
message news:Yotvh.85063$z01.32348@newsfe3-gui.ntli.net...
> benb wrote:
>
>> <snip>

>
> You'll need to write your own comparison function (not tested - YMMV):
>
>
> function my_sort(&$a, &$b)
> {
> global $sort_by;
> if ($a[$sort_by][0]==$b[$sort_by][0]) return 0;
> return ($a[$sort_by][0]<$b[$sort_by][0]) ? -1 : 1;
> }
>
> $sort_by = 'displayname';
> usort($info, 'my_sort');
> // or if you prefer...
> $sort_by = 'mobile';
> usort($info, 'my_sort');
>
>
>
> Note that you don't have to worry about iterating through th array
> yourself,
> or having to code your own quicksort when you discover how slow a bubble
> sort really is...etc.
>
> HTH
>
> C.


Hi Colin,

I tried that code, and it works perfectly in a plain php file. However I'm
trying to add this to a wiki (dokuwiki) so that people can use it to look up
contact info for other staff members. When I c&p the code into the wiki, and
try and run it, the sort function doesn't work. I can't see any reason for
this not to work, I was wondering if you knew of any reason? I have posted
my entire code below, so you can see how it interacts with Active Directory.
I've enable PHP in the wiki config, and it is running the code, and
displaying the records, just not sorting them!

$ldap_host = "ldap://server.domain.local";
$ldap_port = "389";
$base_dn = "OU=Mobile,DC=domain,DC=local";

$filter = "(&(objectClass=user)(objectCategory=person)(CN=*) )";
$ldap_user = "CN=Admin,OU=hq,DC=domain,DC=local";
$ldap_pass = "password";
$inforequired = array("displayname","mobile");

$connect = ldap_connect($ldap_host,$ldap_port)
or exit("Could not connect to LDAP server");

// required to search AD, according to note in PHP manual notes
ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);

$bind = ldap_bind($connect, $ldap_user, $ldap_pass)
or exit("Could not bind to $ldap_host");

//echo "Successful bind to $ldap_host with $bind<br><br>\n";

$read = ldap_search($connect, $base_dn, $filter, $inforequired)
or exit("Unable to search ldap server");

$info = ldap_get_entries($connect, $read);
//echo $info["count"]." entries returned for $filter<br><br>\n";

function my_sort(&$a, &$b)
{
global $sort_by;
if ($a[$sort_by][0]==$b[$sort_by][0]) return 0;
return ($a[$sort_by][0]<$b[$sort_by][0]) ? -1 : 1;
}

$sort_by = 'displayname';
usort($info, 'my_sort');

if($info == 0)
{
print "<p>No information available";
}
else
{
print "<table><tr>"
. "<td><b>Name</b></td>"
. "<td><b>Mobile Number</b></td>"
. "</tr>";

for($i=1;$i<$info[0];$i++)
{
print "<tr><td>" . $info[$i]["displayname"][0] . "</td>";
if (isset($info[$i]["mobile"][0]))
{
print "<td>" . $info[$i]["mobile"][0] . "</td></tr>";
} else {
print "<td>No Number Available</td></tr>";
}

}
print "</table>";
}
ldap_unbind($connect);


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:30 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0