如何在php中按firstName对多维数组进行排序?

时间:2016-06-17 19:28:50

标签: php arrays sorting multidimensional-array laravel-5

我有一个按groupName排序的多维数组(该部分工作正常),每个组内部都有值(groupName,firstName,lastName等)。基本上我试图通过firstName对这个多维数组中的每个组进行排序,我不知道从哪里开始:(。这里是我的多维数组:

      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);

并打印数组我得到了这个:

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
            )

    )

 )

我知道如果它是一个简单的数组(不是多维数组)我可以这样做:

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

有关如何实现这一目标的任何想法吗?非常感谢提前!

2 个答案:

答案 0 :(得分:1)

首先使用适当的项目创建组,然后对每个组进行排序:

$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']);
    });
}

答案 1 :(得分:1)

您需要先按组对行进行分组,然后您可以对每个组进行排序

$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);