dragon_9000 2017-08-25 11:28
浏览 36
已采纳

排序复杂的PHP数组

This is the first part of the array (derived from a JSON feed). The repeating blocks are inside [Attendee], and start at [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
                    )
            )
    )

I want to sort the Attendee[n] by Lastname, a value I can easily echo, but I can't understand how to sort by it. Here's how I echo it:

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

I have searched for existing questions and tried to adapt my case to solutions like these, but without success: Sorting Complex Arrays by Name - Sort multidimensional array alphabetically - PHP Array Complex Sort

  • 写回答

1条回答 默认 最新

  • douzhiling3166 2017-08-25 11:36
    关注

    Your problem is that Lastname isn't in a field of it's own. So you will have to extract it (and pray that your data is consistent).

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?