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 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件