排序复杂的PHP数组

时间:2017-08-25 11:28:06

标签: php sorting multidimensional-array

这是数组的第一部分(源自JSON提要)。重复块位于[Attendee]内部,从[0]开始。

Array
(
[Code] => 1
[Message] => Successfully retrieved data
[Result] => Array
    (
        [Attendee] => Array
            (
                [0] => Array
                    (
                        [CountryCode] => 44
                        [DemographicData] => Array
                            (
                            )

                        [Email] => firstname.lastname@server.com
                        [FreeTextDataCategories] => Array
                            (
                                [AttendeeFreeTextDataCategory] => Array
                                    (
                                        [0] => Array
                                            (
                                                [DataCategoryId] => 165497
                                                [Value] => Firstname
                                            )

                                        [1] => Array
                                            (
                                                [DataCategoryId] => 165498
                                                [Value] => Lastname
                                            )

                                        [2] => Array
                                            (
                                                [DataCategoryId] => 165500
                                                [Value] => Job Title
                                            )

                                        [3] => Array
                                            (
                                                [DataCategoryId] => 165504
                                                [Value] => Array
                                                    (
                                                    )
                                            )
                                    )
                            )

                        [GuestOfAttendeeId] => Array
                            (
                            )

                        [Id] => 3344468
                        [Name] => Firstname Lastname

                        [Password] => Array
                            (
                            )

                        [PhoneNumber] => 123456789
                        [RegisteredVia] => Email
                        [RegistrationDate] => 2017-06-30T11:30:44.313
                        [SessionId] => 111222333
                        [Status] => Registered
                        [StatusDate] => 2017-06-30T11:30:44.313
                    )
            )
    )

我想用Lastname对参加者[n]进行排序,这是一个我很容易回应的值,但我无法理解如何按它排序。 以下是我的回应:

foreach ($result_array['Result']['Attendee'] as $row) {    
echo "<a href=\"mailto:" . $row["Email"] . "\">" . $row["FreeTextDataCategories"]["AttendeeFreeTextDataCategory"][0]["Value"] . " " . $row["FreeTextDataCategories"]["AttendeeFreeTextDataCategory"][1]["Value"]. " </a>";  }

我搜索了现有的问题,并试图让我的案例适应这些解决方案,但没有成功: Sorting Complex Arrays by Name - Sort multidimensional array alphabetically - PHP Array Complex Sort

1 个答案:

答案 0 :(得分:0)

你的问题是Lastname不属于它自己的字段。所以你必须提取它(并祈祷你的数据是一致的)。

function cmp($x, $y) {
    $categoryX = $x['FreeTextDataCategories']['AttendeeFreeTextDataCategory'];
    $lastNameXKey = array_search('165498', array_column($categoryX, 'DataCategoryId'));
    $categoryY = $y['FreeTextDataCategories']['AttendeeFreeTextDataCategory'];
    $lastNameYKey = array_search('165498', array_column($categoryY, 'DataCategoryId'));
    return strcmp($categoryX[$lastNameXKey]['Value'], $categoryY[$lastNameYKey]['Value']);
}

usort($result_array['Result']['Attendee'], 'cmp');