Array groupby  sort in PHP

Posted on 2009-04-27
Last Modified: 2013-12-13

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

Question by:sreejith_y
    1 Comment
    LVL 4

    Accepted Solution

    Is this what you are after?  
    $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++)
    for ($i=0;$i<count($class);$i++)
            echo $class[$i]." ".$name[$i]." ".$place[$i]."\n";

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now