0

I have a multidimensional array that is being sorted by groupName (The part works just fine)and inside each group I have values (groupName, firstName, lastName, etc). Basically I'm trying to sort each group inside this multidimensional array by firstName and I'm not sure where to start :(. Here's my mutidimensional array:

      function group_assoc($array, $key)
        {
            $array_of_groups = array();
            foreach ($array as $value) {
                $array_of_groups[$value[$key]][] = $value;
            }
            return $array_of_groups;
        }


        $somearray = array(
            array('groupName' => 'Group1', 'phoneNumber' => 13037777777, 'firstName'=>'Jeff','lastName' => 'Jeff'),
            array('groupName' => 'Group2', 'phoneNumber' => 13033213453, 'firstName'=>'Zumia','lastName' => 'Brown'),
            array('groupName' => 'Group3', 'phoneNumber' => 13030098342, 'firstName'=>'Junior','lastName' => 'White'),
            array('groupName' => 'Group2', 'phoneNumber' => 13039899231, 'firstName'=>'Ana','lastName' => 'McLwius'),
            array('groupName' => 'Group1', 'phoneNumber' => 13033422109, 'firstName'=>'first','lastName' => 'last'),
            array('groupName' => 'Group3', 'phoneNumber' => 13033222098, 'firstName'=>'Junior','lastName' => 'Smith')
        );


        $account_requests = group_assoc($somearray, 'groupName');
        print_r($account_request);

and printing the array I get this:

Array
(
[Group1] => Array
    (
        [0] => Array
            (
                [groupName] => Group1
                [phoneNumber] => 13037777777
                [firstName] => Mike
                [lastName] => Jeff
            )

        [1] => Array
            (
                [groupName] => Group1
                [phoneNumber] => 13033422109
                [firstName] => Ben
                [lastName] => Morris
            )

    )

[Group2] => Array
    (
        [0] => Array
            (
                [groupName] => Group2
                [phoneNumber] => 13033213453
                [firstName] => Zumia
                [lastName] => Brown
            )

        [1] => Array
            (
                [groupName] => Group2
                [phoneNumber] => 13039899231
                [firstName] => Ana
                [lastName] => McLwius
            )

    )

[Group3] => Array
    (
        [0] => Array
            (
                [groupName] => Group3
                [phoneNumber] => 13030098342
                [firstName] => Junior
                [lastName] => White
            )

        [1] => Array
            (
                [groupName] => Group3
                [phoneNumber] => 13033222098
                [firstName] => Junior
                [lastName] => Smith
            )

    )

 )

I know if it was a simple array (not a multidimensional array) I can just do:

   usort($peopleArray, function ($v1, $v2) {
        return strcmp($v1['firstName'], $v2['firstName']);
    });

Any ideas on how to accomplish this please? Thanks a lot in advance!

Devmix
  • 954
  • 1
  • 19
  • 45

2 Answers2

1

First create groups with proper items, then sort each group:

$groups = array();
foreach ($someArray as $item) {
    $groupName = $item['groupName'];
    if (!isset($groups[$groupName])) {
        $groups[$groupName] = array();
    }
    $groups[$groupName][] = $item;
}

foreach ($groups as &$group) {
    usort($group, function ($v1, $v2) {
        return strcmp($v1['firstName'], $v2['firstName']);
    });
}
u_mulder
  • 51,564
  • 5
  • 39
  • 54
  • are you sure you typed eveything correctly. I see you typed &$group ? Can you update your answer please? Thanks a lot! – Devmix Jun 17 '16 at 20:08
  • I also get an error on line 7 of your code $groups[$group][] = $item; – Devmix Jun 17 '16 at 20:09
  • Everything except `groupName` instead of `group` is correct. – u_mulder Jun 17 '16 at 20:10
  • I don't get each group sorted by firstName :(. Do you do print_r($group) ?? can you please show me your output? Thanks – Devmix Jun 17 '16 at 20:16
  • You're answer is what I needed. However I'm having issues sorting Group1 when the firstName for one of the users has a Capital letter (Ex: Jeff)and the other one a lower case(Ex: first) (It just doesn't sorted correctly). I just updated my question with new values for Group1. Can you test it please?. I will accept you answer as the correct one after this. Thanks a lot!! – Devmix Jun 17 '16 at 20:35
  • Have you tried another comparison function like `strcasecmp` for example? – u_mulder Jun 17 '16 at 20:38
  • I'm reading about it right now. Thanks for all your help. I'm marking your answer as the correct one! – Devmix Jun 17 '16 at 20:42
1

You need to group the rows by group first then yu can sort each group

$someArray = array(
                array('groupName' => 'Group1', 'phoneNumber' => 13037777777, 'firstName'=>'Mike','lastName' => 'Jeff'),
                array('groupName' => 'Group2', 'phoneNumber' => 13033213453, 'firstName'=>'Zumia','lastName' => 'Brown'),
                array('groupName' => 'Group3', 'phoneNumber' => 13030098342, 'firstName'=>'Junior','lastName' => 'White'),
                array('groupName' => 'Group2', 'phoneNumber' => 13039899231, 'firstName'=>'Ana','lastName' => 'McLwius'),
                array('groupName' => 'Group1', 'phoneNumber' => 13033422109, 'firstName'=>'Ben','lastName' => 'Morris'),
                array('groupName' => 'Group3', 'phoneNumber' => 13033222098, 'firstName'=>'Junior','lastName' => 'Smith')
            );

    $sortedArr = array();
    foreach($someArray as $row) {
        $sortedArr[$row['groupName']][] = $row;
    }


    foreach ($sortedArr as &$group) {
        usort($group, function ($v1, $v2) {
            return strcmp($v1['firstName'], $v2['firstName']);
        });
    }

    print_r($sortedArr);
Kld
  • 6,430
  • 3
  • 33
  • 49