?
Solved

Array groupby  sort in PHP

Posted on 2009-04-27
1
Medium Priority
?
801 Views
Last Modified: 2013-12-13
Hi,

Any function availale to do a group by sort an array conents in PHP?
I need to sort the array elements but it should be a group by sort:

Thanks in Adance

$address[0] = array(name->'Name1', place->'Place1',class='1');
$address[1] = array(name->'Name2', place->'Place1'class='2');
$address[2] = array(name->'Name3', place->'Place1',class='1');
$address[3] = array(name->'Name4', place->'Place2',class='2');
$address[4] = array(name->'Name5', place->'Place1'class='3');
$address[5] = array(name->'Name6', place->'Place3',class='1');
$address[6] = array(name->'Name7', place->'Place1',class='4');
$address[7] = array(name->'Name5', place->'Place2',class='1');
 
Here i need to group the array elements on class and list the array elements in name order of slelected class.

Open in new window

0
Comment
Question by:sreejith_y
1 Comment
 
LVL 4

Accepted Solution

by:
joep1978 earned 1000 total points
ID: 24243159
Is this what you are after?  
<?php
 
$address[0] = array('name'=>'Name1', 'place'=>'Place1', 'class'=>'1');
$address[1] = array('name'=>'Name2', 'place'=>'Place1','class'=>'2');
$address[2] = array('name'=>'Name3', 'place'=>'Place1','class'=>'1');
$address[3] = array('name'=>'Name4', 'place'=>'Place2','class'=>'2');
$address[4] = array('name'=>'Name5', 'place'=>'Place1','class'=>'3');
$address[5] = array('name'=>'Name6', 'place'=>'Place3','class'=>'1');
$address[6] = array('name'=>'Name7', 'place'=>'Place1','class'=>'4');
$address[7] = array('name'=>'Name5', 'place'=>'Place2','class'=>'1');
 
for ($i=0;$i<count($address);$i++)
{
        $class[$i]=$address[$i]["class"];
        $name[$i]=$address[$i]["name"];
        $place[$i]=$address[$i]["place"];
}
 
array_multisort($class,SORT_ASC,$name,SORT_ASC,$place,SORT_ASC);
 
for ($i=0;$i<count($class);$i++)
        echo $class[$i]." ".$name[$i]." ".$place[$i]."\n";
 
?>

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question