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 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比