Sorting a Multi-dimensional Array Mutliple Times

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
asteriskman
Forum Newbie
Posts: 2
Joined: Wed May 13, 2009 3:23 pm

Sorting a Multi-dimensional Array Mutliple Times

Post by asteriskman »

Hello all. This is my first post here and I appreciate any help you all can provide to me. I have a multiline file as shown below. If anyone is wondering it is the sip.conf file from an asterisk server. From each entry I would please only the username and caller id field into a standard html table to provide a directory listing. Here is the issue. I need to first sort every entry by the context field and then by the caller id field. I do not need to display the context field.

By using the parse_ini_file function I am able to place this data in an array but cannot sort it the exact way I want. I sure hope someone has a suggestion. Also if it helps the context actually stands for an office. So I am trying to sort by office and then by the persons name to provide a neatly organized directory listing.

Thank you for your time!

;John Smith
[201]
type=friend
context=a
username=201
secret=pass20
callerid=John Smith<4072301234>
host=dynamic
nat=yes
qualify=5000
canreinvite=no
dtmfmode=rfc2833
mailbox=201@abc-vm
disallow=all
allow=g729

;Andy Jones
[202]
type=friend
context=a
username=202
secret=pass202
callerid=Andy Jones<4072301235>
host=dynamic
nat=yes
qualify=5000
canreinvite=no
dtmfmode=rfc2833
mailbox=202@abc-vm
disallow=all
allow=g729

;Mr X
[203]
type=friend
context=b
username=203
secret=pass202
callerid=Mr X<4072301235>
host=dynamic
nat=yes
qualify=5000
canreinvite=no
dtmfmode=rfc2833
mailbox=202@abc-vm
disallow=all
allow=g729
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Sorting a Multi-dimensional Array Mutliple Times

Post by crazycoders »

Code: Select all

 
function sortAsteriskData($a, $b){
if($a['context'] == $b['context']){
return strcmp($a['callerid'], $b['callerid']);
}else{
return strcmp($a['context'], $b['context']);
}
}
 
usort($mydata, 'sortAsteriskData');
 
//Display your $mydata the way you want!
 
Using usort, you can sort using a user-defined sort function, here your sort is pretty easy, so it stands on only a few lines, but sometimes it's much harder than that. Look at: http://ca.php.net/usort for more information...
asteriskman
Forum Newbie
Posts: 2
Joined: Wed May 13, 2009 3:23 pm

Re: Sorting a Multi-dimensional Array Mutliple Times

Post by asteriskman »

Wow thank you for the quick response. It looks like that sorts my array perfectly. Who knew it was that easy? Thank you again!
Post Reply